Sunday 20 August 2017

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
                                           C-Solution   
#include <stdio.h>
#include <string.h>
int main() 
{
    int n,c=0,t=0,i;char a[100000],b[100000];
    scanf("%d",&n);
//    printf("n is  %d",n);
    top:
        scanf("%s",a);
        c=0;
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
             b[c++]=a[i];
       
    }
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
        {
            a[i]=b[--c];
        }
    }
    printf("%s\n",a);
   //    printf(" %d\n",t);
    ++t;
    if(t<n)
    goto top;
    return 0;
}       
          

No comments:

Post a Comment