天天看點

Openjudge-計算概論(A)-單詞翻轉

描述:

輸入一個句子(一行),将句子中的每一個單詞翻轉後輸出。

輸入隻有一行,為一個字元串,不超過500個字元。單詞之間以空格隔開。輸出翻轉每一個單詞後的字元串,單詞之間的空格需與原文一緻。

樣例輸入

hello world      

樣例輸出

olleh dlrow
思路:從頭到尾掃描字元串,遇到空格,翻轉前面的,再回來繼續就得了。
注意:輸入要用gets()函數
代碼如下:      
1 #include<stdio.h>
 2 #include<string.h>
 3 void daoxu(char t[],int j)
 4 {
 5     int i;    
 6     for(i=--j;i>=0;i--)
 7     {
 8         printf("%c",t[i]);
 9     }
10     printf(" ");
11 }
12 int main()
13 {
14     char s[505],temp[500];
15     int i,j=0,len;
16     gets(s);
17     len=strlen(s);
18     for(i=0;i<len;i++)
19     {
20         if(s[i]!=' ')
21         {
22             temp[j++]=s[i];
23         }
24         else if(s[i]==' ')
25         {
26             daoxu(temp,j);
27             j=0;
28         }
29     }
30     daoxu(temp,j);
31     printf("\n");
32     return 0;
33 }      

轉載于:https://www.cnblogs.com/geek-007/p/4296728.html