-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword.cpp
More file actions
40 lines (35 loc) · 861 Bytes
/
word.cpp
File metadata and controls
40 lines (35 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argv, char **argc) {
vector<pair<string, int>> words;
ifstream fin(argc[1]);
string nextword;
while (fin >> nextword) {
bool found = false;
for (auto &[word, occurrence] : words) {
if (word == nextword) {
++occurrence;
found = true;
break;
}
}
if (!found)
words.emplace_back(nextword, 1);
}
if (words.empty()) {
cout << "Input is empty" << endl;
return 0;
}
string word_with_max_occur = words[0].first;
int max_occur = words[0].second;
for (auto &[word, occur] : words)
if (occur > max_occur) {
word_with_max_occur = word;
max_occur = occur;
}
cout << "Word '" << word_with_max_occur << "' appeared " << max_occur
<< " times." << endl;
return 0;
}