Text Reverse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11832 Accepted Submission(s): 4487
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.
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
这个题调试了一段时间才AC,提交了三四次,都是PE!
这个题应该注意什么呢?每个单词之间可以有一个或多个空格,如果用scanf去获得每一行的一个单词,在输出的时候,就可能在应该输出多个空格的地方,只输出了一个空格!导致输出格式的错误!另外,我提交的代码AC了,说明oj提供给这个题测试样例的每一行的单词之间应该是没有制表符的!
以下是代码:
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char *argv[]) 6 { 7 int n, Len, i, flag, j; 8 char s[1005]; 9 scanf( "%d", &n ); 10 getchar(); 11 //printf( "%d\n", n ); 12 while( n-- ) 13 { 14 gets(s); 15 //printf( "%s\n",s ); 16 //getchar(); 17 Len = strlen(s); 18 //printf( "%d\n", Len ); 19 flag = 0; 20 for( i = 0; i < Len; i++ ) 21 if( s[i] == ' ' ) 22 { 23 for( j = i-1; j >= flag; j-- ) 24 printf( "%c", s[j] ); 25 flag = i+1; 26 printf( "%c",s[i] ); 27 } 28 else if( i == Len-1 ) 29 { 30 for( j = i; j >= flag; j-- ) 31 printf( "%c", s[j] ); 32 flag = i+1; 33 } 34 printf( "\n" ); 35 } 36 37 s//ystem("PAUSE"); 38 return 0; 39 }