-
Notifications
You must be signed in to change notification settings - Fork 246
/
11.4.cpp
45 lines (41 loc) · 1.2 KB
/
11.4.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
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>
std::string trimStr(std::string s) {
constexpr char *punctuations{"\"'`:*-_;,.?!()[]{}"};
for (auto &c : s)
if (c >= 'A' && c <= 'Z') c -= 'A' - 'a';
size_t bg = s.find_first_not_of(punctuations);
if (bg == std::string::npos)
return "";
size_t ed = s.find_last_not_of(punctuations);
return s.substr(bg, ed - bg + 1);
}
std::map<std::string, size_t> count_words(std::vector<std::string> &words) {
std::map<std::string, size_t> counts;
for (const auto &w : words)
++counts[trimStr(w)];
return counts;
}
int main() {
std::string filename;
std::cin >> filename;
std::ifstream in(filename);
if (!in.is_open()) {
std::cerr << "Cannot open file: " << filename << std::endl;
return -1;
}
std::istream_iterator<std::string> i_iter(in), eof;
std::vector<std::string> words(i_iter, eof);
for (const auto &w : words)
std::cout << w << std::endl;
auto counts = count_words(words);
for (const auto &w: counts)
std::cout << "\"" << w.first << "\" occurs " << w.second
<< (w.second > 1 ? " times." : " time.") << std::endl;
return 0;
}