P_反转单词
1 //反转I am a boy 为boy a am I 2 #include<iostream> 3 #include<string.h> 4 5 using namespace std; 6 7 void RS(char *begin,char *end) 8 { 9 while(begin<end) 10 { 11 swap(*end,*begin); 12 /*char temp=*begin; 13 *begin=*end; 14 *end=temp;*/ 15 begin++; 16 end--; 17 } 18 } 19 char *Reverse(char *s) 20 { 21 int len=strlen(s); 22 char *es=s+len-1; 23 RS(s,es); 24 char *p1=s; 25 char *p2=s; 26 //判断p2指针是否指向"\0"或" " 27 while(*p2 !='\0') 28 { 29 while(*p2 != '\0' && *p2 != ' ') 30 { 31 p2++; 32 } 33 RS(p1,p2-1); 34 if(*p2 == ' ' && *p2 !='\0') 35 { 36 p2++; 37 p1=p2; 38 } 39 } 40 return s; 41 } 42 43 int main() 44 { 45 char str[500]; 46 gets(str); 47 // Reverse(s); 48 cout<<Reverse(str)<<endl; 49 //puts(s); 50 return 0; 51 }
转载请说明出处!