-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathproblem_16_02_wordFrequencies.cpp
More file actions
32 lines (28 loc) · 1.01 KB
/
problem_16_02_wordFrequencies.cpp
File metadata and controls
32 lines (28 loc) · 1.01 KB
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
#include "problem_16_02_wordFrequencies.h"
namespace chapter_16 {
void trimCopy(std::string& target, const std::string& source) {
for (char c : source) {
char c_lower = static_cast<char>(std::tolower(c));
if (c_lower >= 97 && c_lower <= 122) {
target += c_lower;
}
}
}
void makeDatabase(const std::vector<std::string>& book, std::unordered_map<std::string, int>& database) {
for (const std::string& word : book) {
std::string trimmedWord;
trimCopy(trimmedWord, word);
if (!trimmedWord.empty()) {
if (database.count(trimmedWord) == 0) {
database[trimmedWord] = 1;
}
else {
database[trimmedWord] ++;
}
}
}
}
int wordFrequencies(const std::string& word, const std::unordered_map<std::string, int>& database) {
return database.count(word) ? database.at(word) : 0;
}
}