[Solved] replace a vowel in string with its successor example snow will become snpw [closed]


You should not print str which value’s never changed.

Try to print the StringBuffer's object because it contains the replacements.

System.out.println("New STring is:"+a.toString());

Thanks to @Blip, I did not notice another problem. You added character to the StringBuffer’s object only if the input is a vowel.

Here is what your if test should look like :

if((str.charAt(i)=='a')||(str.charAt(i)=='e')||(str.charAt(i)=='i')||(str.charAt(i)=='o')||(str.charAt(i)=='u'))
{
    b = str.charAt(i);
    b +=1;
    char temp;
    temp = b;
    b = str.charAt(i);
    a.setCharAt(i,temp);
    continue;
}
a.setCharAt(i, str.charAt(i));

Input-Output

aeiou -> bfjpv


That said, here is a changing of your code, easier to write and understand

Scanner in = new Scanner(System.in);
System.out.println("Enter String pls:");
String str = in.nextLine();
StringBuilder output = new StringBuilder();
for (char c : str.toCharArray()){
    if ("aeiou".contains(""+c)){
        output.append((char)(c+1));
    } else {
        output.append(""+c);
    }
}
System.out.println(output.toString());

5

solved replace a vowel in string with its successor example snow will become snpw [closed]