首页 > 代码库 > Reverse Words in a String

Reverse Words in a String

Reverse Words in a String

 

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

代码实现:

 1 # include<stdio.h> 2 void TurnoverS(char* m){ 3     char* k=m; 4     int length = strlen(k); 5     char* n[100];  6     int i=0; 7     for (; i < length; i++) 8     { 9         n[i] = k;10         for (;;k++){11             if (*k == 0)12                 break;13             if (*k ==32&&*(k + 1) != 32)14             { 15                 k++;16                 break;17             }18         }19         if (*k == 0)20             break;21     }22     for (; i >= 0; i--)23         Wc(n[i]);24 }25 26 int Wc (char* p){27     char* k = p;28     for (;;k++){29         if (*k==32)30             break;31         if (*k==0)32             break;33         printf("%c",*k);34     }35     printf(" ");36     return 0;37 }38 void main(){39     char k[100];40     printf("Please input a string of characters that thelength below 100:\n ");41     gets_s(k,98);42     printf("%s\n", k);43     TurnoverS(k);44     system("pause");45 }

效果图:

Reverse Words in a String