题解 | #单词倒排#

单词倒排

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

#include <iostream>
#include <string>
#include <cctype> // 包含 isalpha 函数
#include <sstream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::istringstream;
using std::vector;

void deal_other_character(string& rhs) {
    //将其它字符转换为空格
    auto it = rhs.begin();
    for (; it != rhs.end(); it++) {
        if (!isalpha(*it)) {
            *it = ' ';
        }
    }
}
void test() {
    string str1, word;
    getline(cin, str1);
    deal_other_character(str1);
  //本题最重要的是分割一个句子为单独的单词(string)
    istringstream iss(str1);
    vector<string> vec;
    while (iss >> word) {
        vec.push_back(word);
    }
    for (int i = vec.size() - 1; i >= 0; i--) {
        cout << vec[i] << " ";
    }


}

int main(int argc, char* argv[]) {
    test();
    return 0;

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

昨天 13:50
闽江学院 Java
点赞 评论 收藏
分享
头像
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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