Venky
Venky

Reputation: 11

Replace vowel with next does not work

I have a string input and want to replace every vowel in that string with its next vowel.

However, it does not seem to work correct. I have tried various input like:

venky -> vinky // Correct
ui    -> uo    // Incorrect, expected ao
ai    -> ao    // Incorrect, expected eo

The vowel order is

char[] vowels = { 'a', 'e', 'i', 'o', 'u' };

My code is:

package test;
import java.util.Scanner;

public class Problem1 {
    public static void main(String args[]) {
        Scanner r = new Scanner(System.in);
        String str, res;
        char ch;
        int i, j;
        char[] vowels = { 'a', 'e', 'i', 'o', 'u' };

        System.out.println("Enter a string");
        str = r.nextLine();
        res = str;

        for (i = 0; i < str.length(); i++) {
            ch = str.charAt(i);

            for (j = 0; j < 4; j++) {
                if (ch == 'u') {
                    res = str.replace(ch, 'a');
                    break;
                } else if (ch == vowels[j]) {
                    res = str.replace(ch, vowels[j+1]);
                    break;
                }
            }
        }

        System.out.println("String after replacing the vowels with next vowel : "
            + res);
    }   
}

Upvotes: 1

Views: 1300

Answers (4)

Sundeep
Sundeep

Reputation: 472

res = str.replace(ch, vowels[j+1]);

will replace the string value and put in res variable but on the next time it will not use the old res variable for further modification thats why it is giving wrong answer try changing.

str = str.replace(ch, 'a');
str = str.replace(ch, vowels[j+1]);

and print str instead of len

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Your problem is String.replace will replace all occurrence of a char in your String.

Since you traverse over your String, one original character might be replaced multiple times, for example:

acac -> ecec -> ecic

You will receive ecic instead of ecec.

So I think you can create another String based on the current Strin to avoid the sideeffect of String.replace:

char[] output = new char[str.length()];
for(i = 0; i < str.length(); i++) {
    ch = str.charAt(i);

    for (j = 0; j < 4; j++) {
        if (ch == 'u') {
            output[i] = 'a';
        } else if(ch == vowels[j]) {
            output[i] = vowels[j+1];
        } else {
            output[i] = ch;
        }
    }
}

String res = new String(output);

Upvotes: 1

banshay
banshay

Reputation: 11

You are replacing the vowel in your str variable and saving it to res then you break and continue on your str which has the previous vowel not replaced.

You would need put your response together char by char

Upvotes: 1

StephaneM
StephaneM

Reputation: 4899

Your problem is here:

res = str.replace(ch, vowels[j+1]);

Consider the input ai. The 1st time you pass here you change the value of res to ei, the second time you replace the i in the initial string ai, not in the modified one. So res gets the new value ao.

Anyway, even if you fix this particular case you may hit some other issue with longs words containing many wovels as you replace the first occurence (imagine the case of ae you would get ie). You should build the result one char at a time, with either the unmodified letter or the next wovel.

Upvotes: 1

Related Questions