Reverse vowels in a given string
Last Updated :
13 Jul, 2022
Given a string, your task is to reverse only the vowels of string.
Examples:
Input : hello
Output : holle
Input : hello world
Output : hollo werld
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.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
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' );
}
string reverseVowel(string str)
{
int j=0;
string vowel;
for ( int i=0; str[i]!= '\0' ; i++)
if (isVowel(str[i]))
vowel[j++] = str[i];
for ( int i=0; str[i]!= '\0' ; i++)
if (isVowel(str[i]))
str[i] = vowel[--j] ;
return str;
}
int main()
{
string str = "hello world" ;
cout << reverseVowel(str);
return 0;
}
|
Java
class GFG {
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' );
}
static String reverseVowel(String str1) {
int j = 0 ;
char [] str = str1.toCharArray();
String vowel = "" ;
for ( int i = 0 ; i < str.length; i++) {
if (isVowel(str[i])) {
j++;
vowel += str[i];
}
}
for ( int i = 0 ; i < str.length; i++) {
if (isVowel(str[i])) {
str[i] = vowel.charAt(--j);
}
}
return String.valueOf(str);
}
public static void main(String[] args) {
String str = "hello world" ;
System.out.println(reverseVowel(str));
}
}
|
Python3
def isVowel(c):
if (c = = 'a' or c = = 'A' or c = = 'e' or
c = = 'E' or c = = 'i' or c = = 'I' or
c = = 'o' or c = = 'O' or c = = 'u' or c = = 'U' ):
return True
return False
def reverserVowel(string):
j = 0
vowel = [ 0 ] * len (string)
string = list (string)
for i in range ( len (string)):
if isVowel(string[i]):
vowel[j] = string[i]
j + = 1
for i in range ( len (string)):
if isVowel(string[i]):
j - = 1
string[i] = vowel[j]
return ''.join(string)
if __name__ = = "__main__" :
string = "hello world"
print (reverserVowel(string))
|
C#
using System;
class GFG
{
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' );
}
static String reverseVowel(String str1)
{
int j = 0;
char [] str = str1.ToCharArray();
String vowel = "" ;
for ( int i = 0; i < str.Length; i++)
{
if (isVowel(str[i]))
{
j++;
vowel += str[i];
}
}
for ( int i = 0; i < str.Length; i++)
{
if (isVowel(str[i]))
{
str[i] = vowel[--j];
}
}
return String.Join( "" ,str);
}
public static void Main(String[] args)
{
String str = "hello world" ;
Console.WriteLine(reverseVowel(str));
}
}
|
Javascript
<script>
function isVowel(c) {
return (c == 'a' || c == 'A' || c == 'e'
|| c == 'E' || c == 'i' || c == 'I'
|| c == 'o' || c == 'O' || c == 'u'
|| c == 'U' );
}
function reverseVowel(str1) {
let j = 0;
let str = str1.split( '' );
let vowel = "" ;
for (let i = 0; i < str.length; i++) {
if (isVowel(str[i])) {
j++;
vowel += str[i];
}
}
for (let i = 0; i < str.length; i++) {
if (isVowel(str[i])) {
str[i] = vowel[--j];
}
}
return str.join( "" );
}
let str = "hello world" ;
document.write(reverseVowel(str));
</script>
|
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.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
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' );
}
string reverseVowel(string str)
{
int i = 0;
int j = str.length()-1;
while (i < j)
{
if (!isVowel(str[i]))
{
i++;
continue ;
}
if (!isVowel(str[j]))
{
j--;
continue ;
}
swap(str[i], str[j]);
i++;
j--;
}
return str;
}
int main()
{
string str = "hello world" ;
cout << reverseVowel(str);
return 0;
}
|
Java
class GFG {
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' );
}
static String reverseVowel(String str) {
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 ;
}
char t = str1[i];
str1[i]= str1[j];
str1[j]= t;
i++;
j--;
}
String str2 = String.copyValueOf(str1);
return str2;
}
public static void main(String[] args) {
String str = "hello world" ;
System.out.println(reverseVowel(str));
}
}
|
Python3
def isVowel(c):
return (c = = 'a' or c = = 'A' or c = = 'e' or
c = = 'E' or c = = 'i' or c = = 'I' or
c = = 'o' or c = = 'O' or c = = 'u' or
c = = 'U' )
def reverseVowel( str ):
i = 0
j = len ( str ) - 1
while (i < j):
if not isVowel( str [i]):
i + = 1
continue
if ( not isVowel( str [j])):
j - = 1
continue
str [i], str [j] = str [j], str [i]
i + = 1
j - = 1
return str
if __name__ = = "__main__" :
str = "hello world"
print ( * reverseVowel( list ( str )), sep = "")
|
C#
using System;
class GFG
{
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' );
}
static String reverseVowel(String str)
{
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 ;
}
char t = str1[i];
str1[i]= str1[j];
str1[j]= t;
i++;
j--;
}
String str2 = String.Join( "" ,str1);
return str2;
}
public static void Main(String[] args)
{
String str = "hello world" ;
Console.WriteLine(reverseVowel(str));
}
}
|
Javascript
<script>
function isVowel(c)
{
return (c == 'a' || c == 'A' || c == 'e'
|| c == 'E' || c == 'i' || c == 'I'
|| c == 'o' || c == 'O' || c == 'u'
|| c == 'U' );
}
function reverseVowel(str)
{
let i = 0;
let j = str.length-1;
let str1 = str.split( "" );
while (i < j)
{
if (!isVowel(str1[i]))
{
i++;
continue ;
}
if (!isVowel(str1[j]))
{
j--;
continue ;
}
let t = str1[i];
str1[i]= str1[j];
str1[j]= t;
i++;
j--;
}
let str2 = (str1).join( "" );
return str2;
}
let str = "hello world" ;
document.write(reverseVowel(str));
</script>
|
Time complexity : O(n) where n = length of string
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...