-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ex11_3_4.cpp
63 lines (55 loc) · 1.21 KB
/
ex11_3_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @Yue Wang Aug, 2015
//
// Exercise 11.3:
// Write your own version of the word-counting program.
//
// Exercise 11.4:
// Extend your program to ignore case and punctuation.
// For example, “example.” “example, ” and “Example” should
// all increment the same counter.
//
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cctype>
using std::string;
using std::cin;
using std::cout;
using std::remove_if;
using Map = std::map<std::string, std::size_t>;
//for ex11.3
auto count()
{
Map counts;
for (string w; cin >> w; ++counts[w]);
return counts;
}
//for ex11.4
auto strip(string& str) -> string const&
{
for (auto& ch : str) ch = tolower(ch);
str.erase(remove_if(str.begin(), str.end(), ispunct), str.end());
return str;
}
//for ex11.4
auto strip_and_count()
{
Map counts;
for (string w; cin >> w; ++counts[strip(w)]);
return counts;
}
auto print(Map const& m)
{
for (auto const& kv : m)
cout << kv.first << " : " << kv.second << "\n";
}
int main()
{
cout << "[ex11.3] Enter a few words please:\n";
print(count());
cin.clear();
cout << "[ex11.4] Enter a few words please:\n";
print(strip_and_count());
return 0;
}