方法一、
思路:
1、首先用reverse函数反转所有字符
2、再反转单个单词
3、通过i和j定位单词首位
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str;
getline(cin,str);
reverse(str.begin(), str.end());
for(int i = 0 ; i < str.size() ; i++)
{
int j = i;
while(j < str.size() && str[j] != ' ') j++;
reverse(str.begin() + i, str.begin() + j);
i = j;
}
cout << str << endl;
return 0;
}
方法二、
#include <iostream>
using namespace std;
int main(){
string str,res;
while(cin>>str){
str+=" "+res;
res=str;
}
cout<< res <<endl;
return 0;
}