[华为]单词倒排

链接:https://www.nowcoder.com/questionTerminal/81544a4989df4109b33c2d65037c5836
来源:牛客网

对字符串中的所有单词进行倒排。

说明:

1、每个单词是以26个大写或小写英文字母构成;

2、非构成单词的字符均视为单词间隔符;

3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;

4、每个单词最长20个字母;

输入描述:

输入一行以空格来分隔的句子


输出描述:

输出句子的逆序

输入例子:
I am a student
输出例子:
student a am I
#include<iostream> 
#include<sstream> 
#include<string> 
#include<vector> 
using namespace std; 
int main() 
{ 
   string src,str; 
   vector<string>res; 
   getline(cin,src); 
   stringstream sstream(src); 
   while(sstream>>str)res.push_back(str); 
      for(auto it=res.rbegin();it!=res.rend();++it) 
          if(it==res.rbegin())cout<<*it; 
          else cout<<' '<<*it; 
      cout<<endl; 
      return 0; 
}

  

原文地址:https://www.cnblogs.com/hellochennan/p/6669755.html