-
Notifications
You must be signed in to change notification settings - Fork 4
/
spell.cpp
73 lines (65 loc) · 1.77 KB
/
spell.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
64
65
66
67
68
69
70
71
72
73
// Spells the input using the NATO phonetic alphabet
/* Possible usage scenarios:
spell # spells from the console, end with CTRL+D (UNIX) or CTRL+Z (Windows)
spell "text"
cat file | spell
*/
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
void spell(std::string const& str,
std::unordered_map<char, std::string> const& dict) {
for (auto&& elem : str) {
std::cout << elem;
if (dict.find(std::toupper(elem)) != dict.end()) {
std::cout << " - " << dict.at(std::toupper(elem));
}
std::cout << '\n';
}
}
int main(int argc, char** argv) {
std::unordered_map<char, std::string> dict;
// Convention: use capital letters for the keys
dict['A'] = "Alpha";
dict['B'] = "Bravo";
dict['C'] = "Charlie";
dict['D'] = "Delta";
dict['E'] = "Echo";
dict['F'] = "Foxtrot";
dict['G'] = "Golf";
dict['H'] = "Hotel";
dict['I'] = "India";
dict['J'] = "Juliet";
dict['K'] = "Kilo";
dict['L'] = "Lima";
dict['M'] = "Mike";
dict['N'] = "November";
dict['O'] = "Oscar";
dict['P'] = "Papa";
dict['Q'] = "Quebec";
dict['R'] = "Romeo";
dict['S'] = "Sierra";
dict['T'] = "Tango";
dict['U'] = "Uniform";
dict['V'] = "Victor";
dict['W'] = "Whiskey";
dict['X'] = "X-ray";
dict['Y'] = "Yankee";
dict['Z'] = "Zulu";
if (argc > 2) {
std::cerr << "Usage: " << argv[0] << " [text]\n";
std::exit(EXIT_FAILURE);
}
std::string str;
if (argc == 1) // spells from the standard input
{
while (std::getline(std::cin, str)) {
spell(str, dict);
std::cout << "----- newline -----\n";
}
} else // spells specifed text
{
spell(argv[1], dict);
}
}