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>
int main()
{
int n;
/*scanf返回成功读取的个数,或者EOF,EOF=-1
负数的取反操作等于正数取反加1再取反
-1 -> 0001 -> 1110 + 1 -> 1111 -> 0000*/
while (~scanf("%d", &n)) {
/*scanf读取n后,会留下一个\n在缓冲区,通过
getchar进行消化,不然下面的gets函数会读取\n*/
getchar();
while (n--) {
char s[1010] = "";
gets(s);
int i, j;
int flag = 0;
/*从0开始处理字符串*/
for (i = 0; i < (int)strlen(s); ++i) {
/*碰到空格*/
if (s[i] == ' ') {
/*倒着开始读取每一个字符*/
for (j = i - 1; j >= flag; j--) {
printf("%c", s[j]);
}
printf(" ");
/*标记位置*/
flag = i + 1;
}
}
/*上面i已经遍历到了最后,直接倒着读取字符输出*/
for (j = i - 1; j >= flag; j--) {
printf("%c", s[j]);
}
printf("\n");
}
}
return 0;
}