Consonant Reversal solution
Ritika gives strings to Semona and asks her to perform a consonant reversal on each string such that the indexed positions of its vowels (, , , , and ) are unchanged but all the consonants are reversed. This means that each vowel must be at the same exact index before and after the reversal. Because the value of is very large, Semona wants your help accomplishing this with code!
Given strings, reverse and print each string such that the positions of its vowels remain unchanged but all its consonants are reversed.
Input Format
The first line contains an integer, , the number of strings.
Each of the subsequent lines contains a string, , to be reversed.
Each of the subsequent lines contains a string, , to be reversed.
Constraints
- , where
- Each string contains lowercase letters.
Output Format
For each string received as input, print the consonant reversal of on a new line.
Sample Input
2
abcde
eabafgs
Sample Output
adcbe
easagfb
Explanation
This string has vowels that must stay in place: the at index and the at index . We reverse only the consonants (leaving the vowels in place), and print the result () on a new line.
This string has vowels that must stay in place (at indices , , and ). We reverse only the consonants (leaving the vowels in place), and print the result () on a new line.
#include <stdio.h>
#include <string.h>
int main()
{
int n,i,j,l,k;
char str[50],tmp[50],t;
scanf("%d",&n);
for(k=0;k<n;k++)
{
scanf("%s",&str);
l=strlen(str);
for(j=l-1,i=0;j>=0;)
{
if(str[j]=='a'||str[j]=='e'||str[j]=='i'||str[j]=='o'||str[j]=='u'||str[j]=='A'||str[j]=='E'||str[j]=='I'||str[j]=='O'||str[j]==
'U')
{
tmp[j]=str[j];
j--;
}
else
if(str[i]=='a'||str[i]=='i'||str[i]=='e'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U'){
tmp[i]=str[i];
i++;
}
else
{
t=str[i];
tmp[i]=str[j];
fflush(stdin);
str[j]=t;
i++;
j--;
}
}
tmp[l]='\0';
printf("%s",tmp);
printf("\n");
}
return 0;
}
Comments
Post a Comment