1.题目:
Problem Description
LJ 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.
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
LJSZ
nil eij ihs !uhz
edam yb .fyb
Sample Output
ZSJL
lin jie shi zhu!
made by byf.
Author
2.注意事项:
这题主要要注意一个字符串中有多个空格的情况,还有最后的换行的处理。
3.参考代码:
#include <stdio.h>
#include <string.h>
int main()
{
int t, i, j, l, x, y;
char s[1000];
while (~scanf("%d%*c", &t)) {
while (t--) {
gets(s);
l = strlen(s);
x = 0;
y = 0;
for (i = 0; i < l; i++) {
if (s[i] == ' ') { ///这种情况是有空格的情况
y = i;
for (j = y - 1; s[j] != ' ' && j >= 0; j--)
printf("%c", s[j]);
printf(" ");
} else { ///这是没有空格的情况
x++;
}
}
if (x == l) { ///没有空格就逆序输出整个字符串
for (i = l - 1; i >= 0; i--)
printf("%c", s[i]);
} else { /// 否则处理最后的'\0'的情况
for (j = l - 1; s[j] != ' '; j--)
printf("%c", s[j]);
}
printf("\n");
}
}
return 0;
}