Thursday 1 June 2017

UVA 483 - Word Scramble

Problem: Word Scramble
Write a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves. 

Input
The input file will consist of several lines of several words. Words are contiguous stretches of printable characters delimited by white space. 

Output
The output will consist of the same lines and words as the input file. However, the letters within each word must be reversed. 

Sample Input
I love you.
You love me.
We're a happy family.

Sample Output
I evol .uoy
uoY evol .em
er'eW a yppah .ylimaf

My accepted code is given below:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[1000000];
    long long l,i,c,c2,j;
    while(gets(s))
    {
        l=strlen(s);
        c2=0;
        for(i=0; i<l; i++)
        {
            //cout<<"x";
            if(s[i]==' '||i==l-1)
            {
                //cout<<"x="<<s[i-1];
                if(i==l-1)
                    c=i;
                else
                    c=i-1;

                for(j=c; j>=c2; j--)
                {
                    cout<<s[j];
                    //int x=1;
                }
                if(i!=l-1)
                    cout<<s[i];
                c2=i+1;
            }
        }
         cout<<endl;
    }
    return 0;
}

No comments:

Post a Comment