-2

I want to add "OB" before every vowel. Sample input: "THIS IS A TEST" Sample output: "THOBIS OBIS OBA TOBEST" I have no idea why my code doesn't work:

public static String obify(String test) {
    int x = 0;
    while (x != -1) {
        if (test.charAt(x) == 'A' || test.charAt(x) == 'E' || test.charAt(x) == 'I' || test.charAt(x) == 'O' || test.charAt(x) == 'U') {
            test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));
            x += 3;
        } else {
            x++;
        }
        if (x >= test.length() - 1) {
            x = -1;
        }
    }
    return test;
}
Blah Blah
  • 1
  • 1
  • 1
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Jul 26 '17 at 16:11
  • You could also just use `input.replaceAll("A", "OBA").replaceAll("E", "OBE")` and so on, or directly use regex `input.replaceAll("([AEIOU])", "OB$1")`. Also, what is the output of your current solution? – Zabuzard Jul 26 '17 at 16:12
  • 1
    In what way is it not working? Does it fail with an error? If so, show us the stacktrace. Is the output incorrect? If so, show us what you get. – Andreas Jul 26 '17 at 16:12
  • `replace` method replace all currencies of its first parameter. – talex Jul 26 '17 at 16:23

4 Answers4

4

Perfect scenario for a simple regex

String foo = "hEllo what's up?";
String rep = foo.replaceAll("(?i)([aeiou])", "OB$1");
System.out.println(rep);
baao
  • 71,625
  • 17
  • 143
  • 203
0

You should replace

test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));

with

test = test.substring(0, x) +
        "ob" +
        test.substring(x, x + 1) +
        test.substring(x + 1);

Your problem is that replace act on all occurrences of its first parameter.

When you have "THobIS IS A TEST" and try to replace marked letter you replace both "I" letter. After it you index points to completely wrong position before second "I". Sooner or later you get to it again and situation repeats.

talex
  • 17,973
  • 3
  • 29
  • 66
0

Your problem is in replace call. Documentaion tells that it replaces each substring with new one. So your string grows infinitely: after first replace it is: "THobIS obIS A TEST" after next one is: "THobobIS obobIS A TEST" then "THobobobIS obobobIS A TEST" and so on...

If you change your line

test = test.replace(test.substring(x, x+1), "ob" + test.substring(x, x+1));

to

test = test.substring(0, x) + "ob" + test.substring(x);

it will do the work.

Also you can change while condition to x < test.length() and get rid of second if.

Serge-Zh
  • 26
  • 3
0

I think it is easier this way: For the word "GTREIS": I basically take what was before and after the vowel(including the vowel) and attaching "OB"to the first part, attaching the remaining and finally replacing the original string with the modified one.

public static String  obify(String s)
{ 
    String inloc ="OB",aux="";
    String start="";
    int n=s.length();
    int i=0;

    while (i<n)
    {
        if(s.charAt(i)=='A'|| s.charAt(i)=='E' || s.charAt(i)=='I' ||     s.charAt(i)=='O'||s.charAt(i)=='U' ){
            inloc ="OB";
            start="";
            aux=s.substring(i);//EIS
            System.out.println(aux);
            start=s.substring(0,i);//GTR
            System.out.println(start);
            start=start+inloc;//GTROB
            System.out.println(start);
            start=start+aux;
            s=start;
            i+=3;// here you have to jump over OBE for example and search the next vowel 
            n+=2;
        } else {
            i++;
        }
    }
    return s;
}
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55