题解 | 单词倒排
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream> #include <vector> #include <cctype> #include <algorithm> using namespace std; int main() { string str; getline(cin, str); vector<string> words; string word; for(int i = 0; i < str.size(); ++i) { if(isalpha(str[i])) { word += str[i]; if(i == str.size() - 1){ //最后一个字符不要忘记处理 words.push_back(word); } } else { if(!word.empty()) { words.push_back(word); word = ""; } } } reverse(words.begin(), words.end()); for(const string& word: words) { cout << word << " "; } return 0; }