要求单词倒转,但是单词本身不倒转,其中单词由一个空格隔开
输入:“优快云 is a good place”
输出:”place good a is 优快云“
C++代码如下:
string reverseString(const string & str)
{
string result = str;
int nowindex = 0;
int lastindex = nowindex;
while ((nowindex=nowindex = result.find(" ",nowindex)) != string::npos)
{
reverse(result.begin()+lastindex,result.begin()+nowindex);
nowindex++;
lastindex = nowindex;
}
reverse(result.begin()+lastindex,result.end());
reverse(result.begin(),result.end());
return result;
}