fork download
  1. import java.*;
  2. class Main{
  3. public static void findPattern(String str, String pattern, String replacewith)
  4. {
  5.  
  6.  
  7. char[] chr = str.toCharArray();
  8. char[] ptrn = pattern.toCharArray();
  9. char[] dest = replacewith.toCharArray();
  10.  
  11.  
  12. int i=0, j=0, current=0;
  13. while (current < chr.length - 1)
  14. {
  15. if (chr[i] != ptrn[j])
  16. {
  17. chr[current++] = chr[i++];
  18. j = 0;
  19. }
  20. else
  21. {
  22. if (j == ptrn.length - 1)
  23. {
  24. while (j >= 0)
  25. {
  26. chr[current + j] = dest[j--];
  27. }
  28.  
  29.  
  30. current += ptrn.length;
  31. }
  32. j++;
  33. i++;
  34. }
  35. }
  36. for(int k=0;k<chr.length;k++) System.out.print(chr[k]);
  37. }
  38.  
  39. public static void main(String[] args){
  40. findPattern("ajay is a boy girl boy", "boy", "bwoyz");
  41. }
  42. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
ajay is a bwo girl bwo