#include<iostream> //字符串的反转 using namespace std; void swap( char &cpara1, char &cpara2) { char temp; temp = cpara1; cpara1 = cpara2; cpara2 = temp; } void RevStr( char *src) { char *start=src,*end=src,*ptr=src; while(*ptr++!=' ') { if(*ptr==' '||*ptr==' ') { end=ptr-1; while(start<end) swap(*start++,*end--); start=end=ptr+1; } } start=src; end=ptr-2; while(start<end) { swap(*start++,*end--); } } void RevStrOther( char *src) { char *start=src,*end=src,*ptr=src; while(*ptr++!=' '); end=ptr-2; while(start<end) { swap(*start++,*end--); } start=ptr=src; while(*ptr++!=' ') { if(*ptr==' '||*ptr==' ') { end=ptr-1; while(start<end) swap(*start++,*end--); start=end=ptr+1; } } } int main() { char str[] = "wang dong yu"; //区别 char *str = "wang dong yu"; RevStrOther(str); //RevStr(str); cout<<str<<endl; return 0; }