Reverse vowels in a given string

Given a string, your task is to reverse only the vowels of string.

Examples:

```Input : hello
Output : holle

Input : hello world
Output : hollo werld
```

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

One simple solution is to store all the vowels while scanning the string and placing the vowels in the reverse order in another iteration of string.

C++

 `// C++ program to reverse order of vowels ` `#include ` `using` `namespace` `std; ` ` `  `// utility function to check for vowel ` `bool` `isVowel(``char` `c) ` `{ ` `    ``return` `(c==``'a'` `|| c==``'A'` `|| c==``'e'` `|| ` `            ``c==``'E'` `|| c==``'i'` `|| c==``'I'` `|| ` `            ``c==``'o'` `|| c==``'O'` `|| c==``'u'` `|| ` `            ``c==``'U'``); ` `} ` ` `  `// Function to reverse order of vowels ` `string reverseVowel(string str) ` `{ ` `    ``int` `j=0; ` `    ``// Storing the vowels separately ` `    ``string vowel; ` `    ``for` `(``int` `i=0; str[i]!=``'\0'``; i++) ` `        ``if` `(isVowel(str[i])) ` `            ``vowel[j++] = str[i]; ` ` `  `    ``// Placing the vowels in the reverse ` `    ``// order in the string ` `    ``for` `(``int` `i=0; str[i]!=``'\0'``; i++) ` `        ``if` `(isVowel(str[i])) ` `            ``str[i] = vowel[--j] ; ` ` `  `    ``return` `str; ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``string str = ``"hello world"``; ` `    ``cout << reverseVowel(str); ` `    ``return` `0; ` `} `

Java

 `// Java program to reverse order of vowels ` ` `  `class` `GFG { ` ` `  `// utility function to check for vowel ` `    ``static` `boolean` `isVowel(``char` `c) { ` `        ``return` `(c == ``'a'` `|| c == ``'A'` `|| c == ``'e'` `                ``|| c == ``'E'` `|| c == ``'i'` `|| c == ``'I'` `                ``|| c == ``'o'` `|| c == ``'O'` `|| c == ``'u'` `                ``|| c == ``'U'``); ` `    ``} ` ` `  `// Function to reverse order of vowels ` `    ``static` `String reverseVowel(String str1) { ` `        ``int` `j = ``0``; ` `        ``// Storing the vowels separately ` `        ``char``[] str = str1.toCharArray(); ` `        ``String vowel = ``""``; ` `        ``for` `(``int` `i = ``0``; i < str.length; i++) { ` `            ``if` `(isVowel(str[i])) { ` `                ``j++; ` `                ``vowel += str[i]; ` `            ``} ` `        ``} ` ` `  `        ``// Placing the vowels in the reverse ` `        ``// order in the string ` `        ``for` `(``int` `i = ``0``; i < str.length; i++) { ` `            ``if` `(isVowel(str[i])) { ` `                ``str[i] = vowel.charAt(--j); ` `            ``} ` `        ``} ` ` `  `        ``return` `String.valueOf(str); ` `    ``} ` ` `  `// Driver function ` `    ``public` `static` `void` `main(String[] args) { ` `        ``String str = ``"hello world"``; ` `        ``System.out.println(reverseVowel(str)); ` `    ``} ` `}  ` ` `  `// This code is contributed by princiRaj1992 `

C#

 `// C# program to reverse order of vowels ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// utility function to check for vowel ` `    ``static` `bool` `isVowel(``char` `c)  ` `    ``{ ` `        ``return` `(c == ``'a'` `|| c == ``'A'` `|| c == ``'e'` `                ``|| c == ``'E'` `|| c == ``'i'` `|| c == ``'I'` `                ``|| c == ``'o'` `|| c == ``'O'` `|| c == ``'u'` `                ``|| c == ``'U'``); ` `    ``} ` ` `  `    ``// Function to reverse order of vowels ` `    ``static` `String reverseVowel(String str1) ` `    ``{ ` `        ``int` `j = 0; ` `         `  `        ``// Storing the vowels separately ` `        ``char``[] str = str1.ToCharArray(); ` `        ``String vowel = ``""``; ` `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``{ ` `            ``if` `(isVowel(str[i])) ` `            ``{ ` `                ``j++; ` `                ``vowel += str[i]; ` `            ``} ` `        ``} ` ` `  `        ``// Placing the vowels in the reverse ` `        ``// order in the string ` `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``{ ` `            ``if` `(isVowel(str[i])) ` `            ``{ ` `                ``str[i] = vowel[--j]; ` `            ``} ` `        ``} ` ` `  `        ``return` `String.Join(``""``,str); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``String str = ``"hello world"``; ` `        ``Console.WriteLine(reverseVowel(str)); ` `    ``} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

Output:

```hollo werld
```

Time complexity : O(n) where n = length of string
Auxiliary Space : O(v) where v = number of vowels in string

A better solution is to use two pointers scanning from beginning and end of the array respectively and manipulate vowels pointed by these pointers.

C++

 `// C++ program to reverse order of vowels ` `#include ` `using` `namespace` `std; ` ` `  `// utility function to check for vowel ` `bool` `isVowel(``char` `c) ` `{ ` `    ``return` `(c==``'a'` `|| c==``'A'` `|| c==``'e'` `|| ` `            ``c==``'E'` `|| c==``'i'` `|| c==``'I'` `|| ` `            ``c==``'o'` `|| c==``'O'` `|| c==``'u'` `|| ` `            ``c==``'U'``); ` `} ` ` `  `// Function to reverse order of vowels ` `string reverseVowel(string str) ` `{ ` `    ``// Start two indexes from two corners ` `    ``// and move toward each other ` `    ``int` `i = 0; ` `    ``int` `j = str.length()-1; ` `    ``while` `(i < j) ` `    ``{ ` `        ``if` `(!isVowel(str[i])) ` `        ``{ ` `            ``i++; ` `            ``continue``; ` `        ``} ` `        ``if` `(!isVowel(str[j])) ` `        ``{ ` `            ``j--; ` `            ``continue``; ` `        ``} ` ` `  `        ``// swapping ` `        ``swap(str[i], str[j]); ` ` `  `        ``i++; ` `        ``j--; ` `    ``} ` ` `  `    ``return` `str; ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``string str = ``"hello world"``; ` `    ``cout << reverseVowel(str); ` `    ``return` `0; ` `} `

Java

 `// Java program to reverse order of vowels ` ` `  `class` `GFG { ` ` `  `// utility function to check for vowel ` `    ``static` `boolean` `isVowel(``char` `c) { ` `        ``return` `(c == ``'a'` `|| c == ``'A'` `|| c == ``'e'` `                ``|| c == ``'E'` `|| c == ``'i'` `|| c == ``'I'` `                ``|| c == ``'o'` `|| c == ``'O'` `|| c == ``'u'` `                ``|| c == ``'U'``); ` `    ``} ` ` `  `// Function to reverse order of vowels ` `static` `String reverseVowel(String str) { ` `    ``// Start two indexes from two corners ` `    ``// and move toward each other ` `    ``int` `i = ``0``; ` `    ``int` `j = str.length()-``1``; ` `    ``char``[] str1 = str.toCharArray(); ` `    ``while` `(i < j) ` `    ``{ ` `        ``if` `(!isVowel(str1[i])) ` `        ``{ ` `            ``i++; ` `            ``continue``; ` `        ``} ` `        ``if` `(!isVowel(str1[j])) ` `        ``{ ` `            ``j--; ` `            ``continue``; ` `        ``} ` ` `  `        ``// swapping ` `        ``char` `t = str1[i]; ` `        ``str1[i]= str1[j]; ` `        ``str1[j]= t; ` `         `  ` `  `        ``i++; ` `        ``j--; ` `    ``} ` `    ``String str2 = String.copyValueOf(str1); ` `    ``return` `str2; ` `} ` ` `  `// Driver function ` `    ``public` `static` `void` `main(String[] args) { ` `        ``String str = ``"hello world"``; ` `        ``System.out.println(reverseVowel(str)); ` `    ``} ` `} `

Output:

```hollo werld
```

Time complexity : O(n) where n = length of string
Auxiliary Space : O(1)

This article is contributed by Gaurav Ahirwar and Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.