Jamie.L
Jamie.L

Reputation: 15

Duplicate a vowel in String and add a String in-between

I have to write a method that takes in a String and returns a new string that duplicates all vowels and puts a "b" in between. Only exception goes for diphtongs where "ab" should be put in front of the diphtong.

For example: "hello" would return "hebellobo" "hearing" would return "habearing"

I've experimented with my code for hours but I am not getting anything done. Well, not anything, but can't make it run properly for vowels and didn't get to the diphtongs at all. Here is my code:

static Scanner sc = new Scanner(System.in);

public static void main(String[] args)
{
    System.out.print("Enter a string: ");
    String s = sc.nextLine();
    String originalString = s;

    for (int i = 0; i < s.length(); i++)
    {
        char c = s.charAt(i);

        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u'))
        {

            String front = s.substring(0, i);
            String back =  s.substring(i + 1);

            s = front + c + "b" + back;
        }
    }
    System.out.println(originalString);
    System.out.println(s);
 }

Grateful for any help !

Thanks to your help I now have the following code (without Scanner):

public static boolean isVowel(char c) {
    // TODO exercise 1 task b) part 1

    if (c == 'a' || c == 'A' || c == 'Ä' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O'
            || c == 'Ö' || c == 'u' || c == 'U' || c == 'Ü') {
        return true;
    } else {
        return false;
    }
}

public static String toB(String text) {
    // TODO exercise 1 task b) part 2

    StringBuilder b = new StringBuilder();

    for (int i = 0; i < text.length() - 1; i++) {
        char current = text.charAt(i);
        char next = text.charAt(i + 1);

        if (isVowel(current)) {
            if (isVowel(next)) {
                // 1 - Is a vowel followed by a vowel
                // Prepend b
                b.append("b");
                // Write current
                b.append(current);
                // Write next
                b.append(next);
                i++; // Skip next vowel
            } else {
                // 2 - Is a vowel followed by a consonant
                b.append(current);
                b.append("b");
                b.append(current);
            }
        } else {
            // 3 - Is a consonant
            b.append(current);
}
    }
    for (int i = 0; i < text.length() - 1; i++) {
     char last = text.charAt(text.length() - 1);
     char current = text.charAt(i);
     if (isVowel(last)) {
        // Case 1
        b.append(current);
        b.append("b");
        b.append(current);

        // Case 2 is not possible for last letter
      } else {
         // Case 3
         b.append(last);
      }

    }
     // Here b.toString() is the required string
     return b.toString();
    }

If you put in the word "Mother" for example, the ouput is "Mobotheberrrrr" which is perfectly fine, except that it repeats the last letter 'r' for some reason. Input "Goal" results in Output "Gboalll" unfortunately.

Upvotes: 1

Views: 895

Answers (2)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

You need to know the current letter and also the next letter.

In your code you take in consideration only the current letter.

Here is a skeleton code to solve the problem. Basically you need to check:

  • If the current letter is a vowel followed by a vowel
  • If the current letter is a vowel followed by a consonant
  • If the current letter is a consonant

    String originalString = ...
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < s.length() - 1; i++) {
        char current = s.charAt(i);
        char next = s.charAt(i + 1); 
    
        if (isVowel(current)) {
           if (isVowel(next)) {
              // 1 - Is a vowel followed by a vowel
              // Prepend b
              b.append("b");
              // Write current
              b.append(current);
              // Write next
              b.append(next);
              i++; // Skip next vowel
           } else {
              // 2 - Is a vowel followed by a consonant
              b.append(current);
              b.append("b");
              b.append(current);
           }
        } else {
            // 3 - Is a consonant
           b.append(current);
        }
     }
    
     char last = s.charAt(s.length() - 1);
     if (isVowel(last)) {
        // Case 1
        b.append(current);
        b.append("b");
        b.append(current);
    
        // Case 2 is not possible for last letter
      } else {
         // Case 3
         b.append(last);
      }
    
    
     // Here b.toString() is the required string
    

Please consider this only as a skeleton, in particular:

  • check for border conditions
  • implements the method isVowel
  • check for null and empty strings

Note: the use of StringBuilder is only for performance reasons, using directly the String s will give the same result

Upvotes: 1

7H3_H4CK3R
7H3_H4CK3R

Reputation: 143

My best guess is make a chain of replaceAlls because you are basically replacing vowels with duplicates and bs so try something like so:

String original = something;
String adjusted = original.replaceAll("ea","abea").replaceAll("a","aba").replaceAll(...)...;

And just fill in the rules. Make sure to check diphthongs before checking single vowels or they will be treated as two single vowels

Upvotes: 0

Related Questions