fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int indexOfIgnoreCase(String str, String find, int start) {
  11. for(int i = start; i < str.length(); i++) {
  12. if(str.substring(i, i + find.length()).equalsIgnoreCase(find)) {
  13. return i;
  14. }
  15. }
  16. return -1;
  17. }
  18.  
  19. static void solve(String sentence) {
  20. String find = "java";
  21. String replace = "JAVA";
  22. int index = 0;
  23. while(index < sentence.length()) {
  24. index = indexOfIgnoreCase(sentence, find, index);
  25. if(index == -1) {
  26. break;
  27. }
  28. sentence = sentence.substring(0, index) + // string up to found word
  29. replace + // replace found word
  30. sentence.substring(index + find.length()); // remaining part of the string
  31. index += find.length();
  32. }
  33. System.out.println(sentence);
  34. }
  35.  
  36. public static void main (String[] args) throws java.lang.Exception
  37. {
  38. String sentence = "Java, JAva, java, JaVa, JAVa";
  39. solve(sentence);
  40. }
  41. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
JAVA, JAVA, JAVA, JAVA, JAVA