//求字符串长度 int strlen(char*s) ...{ int i; for(i=0;s!='\0';i++) ; return i; } //反转字符串的某一断子串 void Reverse(char*s,int ben,int end) ...{ int len=end-ben+1; for(int i=0;i<len/2;i++) ...{ char c=s[ben+i]; s[ben+i]=s[ben+len-i-1]; s[ben+len-i-1]=c; } }
void ReverseWord(char*s) ...{ //先将整个字符串反转 Reverse(s,0,strlen(s)-1); bool isword=false; int i=0; int j=0; int k=0;
//分离单词 while (s[k] !='\0') ...{ while(s[k] =='') k++; while ((s[k] !='\0') && (s[k] !='')) ...{ i++,k++; } if (i ==0) return ; Reverse(s, k - i, k -1); i =0; } }
int main() ...{ char s[]="I love you"; printf("Before Reverse:%s\n",s); ReverseWord(s); printf("After Reverse:%s\n",s); return0; }