Find your content:

Search form

You are here

Reverse vowels in a given string

 
Share

Reverse vowels in a given string

In a given string, reverse the vowels alone. Below is a Java standard two pointer solution

Example :

Input : Hello

Output : holle

Input : Hello! Welcome to Help interview.

Output :Helli! Welcime to Help ontervoew.

 

public class VowelReverse {

  public static String reverseVowel(String inpString) {

    String vowels = "AEIOUaeiou";

    int i = 0;

    int j = inpString.length() - 1;

    char[] inpCharArray = inpString.toCharArray();

    while (i < j) {

      if (vowels.indexOf(inpCharArray[i]) == -1) {

        i++;

        continue;

      }

      if (vowels.indexOf(inpCharArray[j]) == -1) {

        j--;

        continue;

      }

      swap(inpCharArray, i, j);

      i++;

      j--;

    }

    return inpCharArray.toString();

  }

 

  private static void swap(char[] inpCharArray, int first, int second) {

    char tmp = inpCharArray[first];

    inpCharArray[first] = inpCharArray[second];

    inpCharArray[second] = tmp;

  }

}

My Block Status

My Block Content