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.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
Author
Ignatius.L
解题思路:
注意:用getchar()消去录入的换行,用gets()函数直接录入字符串
录入字符串之后设置两个变量uper 和 lower ,以空格为单位反转输出字符串
源代码:
# include <stdio.h>
# include <string.h>
char s[1001];
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
getchar();//接收=0,传过来的空行
while (n--)
{
gets(s);
int lower=0,uper=0;
int i=0,j=0;
int len=strlen(s);
for (i=0;i<len;i++)
{
while ((s[i]!=' ')&&(s[i]!='\0'))
i++;
uper=i-1;
for (j=uper;j>=lower;j--)
printf("%c",s[j]);
lower=uper+2;
if (s[i]==' ')
printf(" ");
}
printf("\n");
}
}
return 0;
}