Solution of 483 - Word Scramble

Problem Description
source:https://uva.onlinejudge.org/external/4/483.html

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

Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main()
{
    int i,j,k;
    string input;
    //freopen("in.txt","r",stdin);
    while(getline(cin,input))
    {
        k=0;
        for(i=0;input[i]!='\0';i++)
        {
            if(input[i]==' ')
            {
                for(j=i-1;j>=k;j--)
                {
                    printf("%c",input[j]);
                }
                printf(" ");
                k=i+1;
            }
        }
        for(j=i-1;j>=k;j--)
        {
            printf("%c",input[j]);
        }
        printf("\n");
    }
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience