题解 | #单词倒排#

单词倒排

https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

#include <iostream>
using namespace std;
#include <string>
#include <sstream>
#include <cctype>
#include <stack>
int main()
{
    string str;
    getline(cin, str);
    for (int i = 0; i <= str.size(); i++)
    {
        if (!isalpha(str[i]))
        {
            str[i] = ' ';
        }
    }

    istringstream is(str); 
    string word;
    stack<string>s;
    while(is>>word){
        s.push(word);
    }
    while(!s.empty()){
        cout << s.top() << " ";
        s.pop();
    }
}

和HJ13比,主要是多了一项对特殊字符的检查,用cctype完成

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务