欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串(5)——统计单词个数

程序员文章站 2024-03-15 08:59:59
...

重点:set和istringstream的应用
字符串(5)——统计单词个数

#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
using namespace std;
int n;
string s;
const set<string> mod{"the", "a", "an", "of", "for", "and"};
int main() {
    cin >> n;
    cin.ignore();
    for (int i = 0; i < n; i++) {
        getline(cin, s);
        for (auto &i : s)
            i = tolower(i);
        string temp;
        istringstream is(s);
        int cnt = 0;
        while (is >> temp) {
            if (mod.find(temp) == mod.end())
                cnt++;
        }
        cout << "case #" << i << ":\n" << cnt << '\n';
    }

    return 0;
}

补充资料:
https://blog.csdn.net/Jacky_chenjp/article/details/70233212?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task