题解 | #单词倒排#

单词倒排

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")

全部评论

相关推荐

求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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