Description
For each list of words, output a line with each word reversed without changing the order of the words.
Input
The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.
Output
For each test case, print the output on one line.
Sample Input
3 I am happy today To be or not to be I want to win the practice contest
Sample Output
I ma yppah yadot oT eb ro ton ot eb I tnaw ot niw eht ecitcarp tsetnoc
题目意思:将每个单词的字符进行反转。
最初代码:
#include<stdio.h> #include<string.h> int main() { int n,m,i,j,k; char s[1000],x[1000]; scanf("%d",&n); getchar(); while(n--) { gets(s); m=strlen(s); k=0; for(i=0; i<=m; i++) { if(s[i]!=‘ ‘&&s[i]!=0) { x[k]=s[i]; k++; } else { for(j=k-1; j>=0; j--) printf("%c",x[j]);///逆向输出 if(s[i]!=0) printf(" "); k=0; } } printf("\n"); } return 0; }
这个代码是一个数组向另外一个数组中倒数,时间复杂度上还是存在着问题,修改之后的新代码如下:
1 #include<stdio.h>///时间复杂度更小的另外一种方法 2 #include<string.h> 3 int main() 4 { 5 int t,m,i,j; 6 char s[10000]; 7 scanf("%d",&t); 8 getchar(); 9 while(t--) 10 { 11 gets(s); 12 m=strlen(s); 13 for(i=0; i<m; i++) 14 { 15 if(s[i]==‘ ‘) 16 { 17 for(j=i-1; j>=0&&s[j]!=‘ ‘; j--) 18 { 19 printf("%c",s[j]); 20 } 21 printf(" "); 22 } 23 } 24 for(i=m-1; i>=0&&s[i]!=‘ ‘; i--) 25 { 26 printf("%c",s[i]); 27 } 28 printf("\n"); 29 } 30 return 0; 31 }