计算文本文件C ++中特定单词的出现次数

时间:2017-09-13 01:10:10

标签: c++

代码用于计算您从文本文件中键入的特定单词的次数。         到目前为止我的代码:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main()
    {
        string word;
        string wif;
        fstream file("words.txt");
        int nTimes = 0;
        cin >> word;
        while (file >> wif) {
            cout << wif << " ";
            if (wif == word) {
                ++nTimes;
            }
        }
        cout << nTimes << endl;
        return 0;
    }

代码有效,但不是我所期望的。 例如:文件总共有6个“多”字,但输出为4,有些字是这样的:很多,很多? 是不是因为字符而添加了这些单词,而且?

0 个答案:

没有答案
相关问题