- 使用getline获取一行字符串
- 使用空格“ ”代替字符串中非字母数字;
- 使用stringstream对处理过后的数据进行逐个单词读取,会自动跳过空格;
- 使用vector储存每个单词,然后用reverse函数倒排;
- 输出单词和空格,注意最后一个单词没有空格,最后输出换行符。
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
while(getline(cin,s)){
for(auto &c: s){// 如果接受的是一个引用,那么一定会更改之前东西
if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
{
continue;
}
else
{
c = ' ';
}
}
stringstream ss(s);//可以自动跳过空格进行读取,相当于空了一个格
string word;
vector<string> vec;
while(ss>>word){
vec.push_back(word);
}
reverse(vec.begin(),vec.end()); // 记住这个技巧,数组是可以reverse的。
for(int i = 0 ; i< vec.size();i++){
cout<<vec[i];
if(i!=vec.size()-1){
cout<<' ';
}
}
cout<<endl;
}
return 0;
}