TechoSagar: Reversing the vowels

Search

Google Alert - jobs

Reversing the vowels

Given a string, reverse only the vowels present in it and print the resulting string.

Input: First line of the input file contains an integer T denoting the number of test cases. Then T test cases follow. Each test case has a single line containing a string.

Output: Corresponding to each test case, output the string with vowels reversed.

Example:
Input:
4
geeksforgeeks
practice
wannacry
ransomware

Output:
geeksforgeeks
prectica
wannacry
rensamwora
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.length();
//cout<<l<<endl;
int i;
int b[100000];
int k=0;
for(i=0; i<l; i++)
{
if(s[i]=='a'||s[i]=='i'|| s[i]=='o'||s[i]=='e'||s[i]=='u')
{    
b[k]=i;
k++;
}
if(k%2==0)
{
for(i=0; i<k/2; i++)
{   char temp=s[b[i]];
//cout<<s[b[i]];
s[b[i]]=s[b[k-i-1]];
s[b[k-i-1]]=temp;
}
 
 }
 else
 {
  for(i=0; i<=k/2; i++)
{
char temp=s[b[i]];
//cout<<temp;
s[b[i]]=s[b[k-i-1]];
s[b[k-i-1]]=temp;
}
 
 }
cout<<s<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook