#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++!='\0') { if(*ptr==' '||*ptr=='\0') { 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++!='\0'); end=ptr-2; while(start<end) { swap(*start++,*end--); } start=ptr=src; while(*ptr++!='\0') { if(*ptr==' '||*ptr=='\0') { 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; }