jeudi 29 novembre 2018

How to find the most repeated word?

How can make the program to print the most repeated word? For now it prints as follow:

Input: apple banana apple apple

Output:

apple appeared 3 times

banana appeared 1 times

int main() {
        string words;
        vector<string> stringHolder;

        while (cin >> words)
        {
            stringHolder.push_back(words);
        }
        sort(stringHolder.begin(), stringHolder.end());

        int vSize = stringHolder.size();
        if (vSize == 0) {
            cout << " no words ";
        }
        int wordCount = 1;
        words = stringHolder[0];
        for (int i = 1; i<vSize; i++) {
            if (words != stringHolder[i]) {
                cout << words << " appeared " << wordCount << " times" << endl;
                wordCount = 0;
                words = stringHolder[i];
            }
            wordCount++;
        }
        cout << words << " appeared " << wordCount << " times" << endl;
        system("pause");
    }

Aucun commentaire:

Enregistrer un commentaire