笔试题-单词反转
先反转整个字符串,然后反转每个单词
string func(string & s,int a,int b) { b--; while(a<b) { s[a]=s[a]^s[b]; s[b]=s[a]^s[b]; s[a]=s[a]^s[b]; a++,b--; } return s; } int main(int argc, char *argv[]) { string s= "i like netease Game"; int a= 0; int b = s.size(); func(s,a,b);//交换全部 cout<<s<<endl; //交换单词 int ii= 0; for(int i=0;i<s.size();i++) { if(s[i]==' ') { func(s,ii,i); cout<<s<<endl; ii=i+1; } } func(s,ii,s.size()); cout<<s<<endl; return 0; }