-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWovelCode.cpp
More file actions
40 lines (34 loc) · 1.01 KB
/
WovelCode.cpp
File metadata and controls
40 lines (34 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
32
33
34
35
36
37
38
39
40
//https://www.codewars.com/kata/53697be005f803751e0015aa
//write function to replace all the lowercase vowels in a given string with numbers and function to turn to back
#include <string>
#include <algorithm>
#include <unordered_map>
const std::unordered_map<char, char> encode_replacements = {
{'a', '1'},
{'e', '2'},
{'i', '3'},
{'o', '4'},
{'u', '5'},
};
const std::unordered_map<char, char> decode_replacements = {
{'1', 'a'},
{'2', 'e'},
{'3', 'i'},
{'4', 'o'},
{'5', 'u'},
};
std::string impl(const std::string& str, const std::unordered_map<char, char>& repl) {
std::string output{str};
std::for_each(output.begin(), output.end(), [&repl](char& ch) {
if (repl.find(ch) != repl.end()) {
ch = repl.at(ch);
}
});
return output;
}
std::string encode(const std::string& str) {
return impl(str, encode_replacements);
}
std::string decode(const std::string& str) {
return impl(str, decode_replacements);
};