题解 | 单词倒排

单词倒排

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;
}

全部评论

相关推荐

前段时间投boss,实在没绷住,就发出来吧
测开小登的自我救赎:这种就别较真了,感觉应该是那种吃上了学历贬值的时代红利感觉自己也能找一堆92硕士的边角料小公司吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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