输入一个英文名字,翻转句子中单词的顺序。要求单词内字符的顺序不变,句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如,若输入“”I am a student.“”则输出“”student. a am I“”
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string reverse_string(string str)
{
int len = str.length();
string result = "";
int pri = 0;
for(int i = 0;i < len;)
{
while(str[i+1] != ' '&& i < len-1)
i++;
string temp = str.substr(pri,i-pri+1);
reverse(temp.begin(),temp.end());
result+= " ";
result+= temp;
i++;
pri = i+1;
}
reverse(result.begin(),result.end());
return result;
}
int main()
{
string a = "I am a student.";
cout<<reverse_string(a)<<endl;
}