Reversed Words
Time Limit: 2000 ms Memory Limit: 131072 KiB
Problem Description
Some aliens are learning English. They have a very strange way in writing that they revered every word in the sentence but keep all the words in common order. For example when they want to write “one two three”, they will write down “eno owt eerht”.
Now we’ve got some sentence written by these aliens, translate them! And maybe we will know some of their secrets!
Input
Multiple test cases. The first line contains a positive integer T (T <= 1000), indicating the number of test cases.
For each test cases, there will be one line contains only lower case letters and spaces. The length of each line will be no more than 10000. Test cases which are longer than 5000 will be less than 50. Continuous letters are seen as a word, words are separated by spaces. There won’t be two adjacent spaces in the input. Space won’t be the first or the last character.
Output
One line per case, the translated sentence.
Example Input
2
eno owt eerht
abcde
Example Output
one two three
edcba
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/3570.html
本题结合样例容易得知题意是翻转每个单词
参考代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 10010
char s[N];
int main(void)
{
int i, j, k;
int t, len;
scanf("%d", &t);
getchar();
while(t--){
gets(s);
len = strlen(s);
for(i = 0; i < len; i++){
if(s[i] == ' '){
for(j = i-1; j >= 0 && s[j] != ' '; j--){
putchar(s[j]);
}
printf(" ");
}
}
for(i = len-1; i >=0 && s[i] != ' '; i--)
putchar(s[i]);
printf("\n");
}
return 0;
}