标签:
字符串翻转,单词内不翻转。如I am from Chongqing->Chongqing from am I
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 void reverse(char *str, int beg, int end); 6 void reverseSentece(char *str); 7 int main() 8 { 9 char str[128]; 10 gets_s(str,128); 11 reverseSentece(str); 12 puts(str); 13 system("pause"); 14 return 0; 15 } 16 17 void reverse(char *str, int beg, int end) 18 { 19 char temp; 20 if(str == NULL) 21 return; 22 while(beg <= end) 23 { 24 temp = str[beg]; 25 str[beg] = str[end]; 26 str[end] = temp; 27 beg++; 28 end--; 29 } 30 } 31 32 void reverseSentece(char *str) 33 { 34 int beg = 0; 35 int end = 0; 36 if(str == NULL) 37 return; 38 //翻转整个句子 39 reverse(str,0,strlen(str)-1); 40 //局部翻转 41 while(str[end] != ‘\0‘) 42 { 43 //找到单词的结束 44 while(str[end] != ‘\0‘ && str[end] != ‘ ‘) 45 ++end; 46 //翻转一个单词 47 reverse(str,beg,end-1); 48 if(str[end] == ‘\0‘) 49 break; 50 ++end; 51 beg = end; 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/cpsmile/p/4776835.html