题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string line;
string word;
stack<string> s;
getline(cin, line);
for (char c : line)
{
if (isalpha(c))
{
word += c;
}
else if (!word.empty()) //单词入栈并清空字符串
{
s.push(word);
word.clear();
}
}
if (!word.empty()) // word 不为空则推入栈
s.push(word);
while (!s.empty())
{
cout << s.top();
s.pop();
if (!s.empty()) // 栈未空时,单词之间输出空格
cout << " ";
}
cout << endl;
return 0;
}


