Text Reverse
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K(Java/Others)Total Submission(s): 7906 Accepted Submission(s):3015
Problem Description
Ignatius likes to write words in reverseway. Given a single line of text which is written by Ignatius, youshould reverse all the words and then output them.
Input
The input contains several test cases.The first line of the input is a single integer T which is thenumber of test cases. T test cases follow.
Each test case contains a single line with several words. Therewill be at most 1000 characters in a line.
Each test case contains a single line with several words. Therewill be at most 1000 characters in a line.
Output
For each test case, you should outputthe text which is processed.
Sample Input
3 olleh!dlrow m'I morf .udh I ekil .mca
Sample Output
helloworld! I'm from hdu. I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, thenyou may use gets() to read a line and process it.
Author
Ignatius.L
#include<iostream>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main(){
intt,i,j,k,r,s;
charstr[1005],out[1005];
scanf("%d",&t);
getchar();
while(t--){
memset(str,'\0',sizeof(str));
memset(out,'\0',sizeof(out));
gets(str);
for(i=0,k=0;i<=strlen(str);i++)
if(str[i]==' '||str[i]=='\0'){
r=i-k;
for(j=i-1,s=0;s<r;s++,j--,k++)
out[k]=str[j];
if(k<strlen(str))
out[k]=' ';
k++;
}
puts(out);
}
return0;
}