-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.04.cpp
53 lines (49 loc) · 985 Bytes
/
11.04.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <set>
#include <map>
#include <iostream>
#include <string>
#include <cstddef>
#include <cctype>
using std::map;
using std::set;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::tolower;
using std::ispunct;
string lowercase(string s) {
for (char &c : s) {
c = tolower(c);
}
return s;
}
string removePunct(string s) {
string::iterator iter = s.begin();
while (iter != s.end()) {
cout << *iter << ' ';
if (ispunct(*iter)) {
iter = s.erase(iter);
} else {
++iter;
}
}
cout << s << endl;
return s;
}
int main() {
map<string, size_t> word_count;
set<string> exclude { "the", "but", "and", "or", "an", "a"};
string word;
while (cin >> word) {
word = removePunct(word);
word = lowercase(word);
if (exclude.find(word) == exclude.end()) {
++word_count[word];
}
}
for (const auto &w : word_count) {
cout << w.first << " occurs " << w.second << ((w.second > 1) ? " times." : " time.") << endl;
}
return 0;
}