problem description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should 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 the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
output
For each test case, you should output the text which is processed.
sample input
3
olleh !dlrow
m'I morf .udh
I ekil .mca
sample output
hello world!
I'm from hdu.
I like acm.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
char c=getchar(); //注意用getchar获得输入t后的\n否则会出错
char a[1001];
for(int i=0;i<t;i++)
{
gets(a);//使用gets获得一行,如果用scanf或者cin,空格后的剩余单词不会被赋值到a中
int begin=0;
for(int j=0;j<=strlen(a);j++)
{
if(a[j]==' '||j==strlen(a))
{
reverse(a+begin,a+j);//使用algorithm中的逆转函数
begin=j+1;
}
}
puts(a);
}
}
return 0;
}