-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundex.cpp
48 lines (40 loc) · 1.33 KB
/
soundex.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
#include <iostream>
#include <string>
int main() {
std::string in, out;
std::cin >> in; // input string
out += in[0]; // add first letter to output string
in.erase(0, 1); // erase first char in input string
int pos = 0;
// create strings to check char similarity
std::string one = "bfpv", two = "cgjkqsxz",
three = "dt", four = "l", five = "mn", six = "r";
// filling in the output vector
for (char c : in) {
if (one.find(c) != std::string::npos && out[pos] != '1') {
out += "1";
pos++;
} else if (two.find(c) != std::string::npos && out[pos] != '2') {
out += "2";
pos++;
} else if (three.find(c) != std::string::npos && out[pos] != '3') {
out += "3";
pos++;
} else if (four.find(c) != std::string::npos && out[pos] != '4') {
out += "4";
pos++;
} else if (five.find(c) != std::string::npos && out[pos] != '5') {
out += "5";
pos++;
} else if (six.find(c) != std::string::npos && out[pos] != '6') {
out += "6";
pos++;
}
}
// if length of the output vector is less than 4, we will fullfill it with zeros
while (out.length() < 4) {
out += "0";
}
std::cout << out.substr(0, 4);
return 0;
}