-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
87 lines (79 loc) · 1.81 KB
/
Main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cctype>
#include "console.h"
#include "error.h"
#include "simpio.h"
#include "strlib.h"
using namespace std;
// converts letter to digit using the Soundex table
int SoundexConvertToDigit(char letter)
{
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'H':
case 'W':
case 'Y':
return 0;
case 'B':
case 'F':
case 'P':
case 'V':
return 1;
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z':
return 2;
case 'D':
case 'T':
return 3;
case 'M':
case 'N':
return 4;
case 'L':
return 5;
case 'R':
return 6;
default:
error("Letter not found");
}
}
string SoundexCode(string surname)
{
surname = toUpperCase(surname);
string res = charToString(surname[0]);
for (unsigned int i = 1; i < surname.size(); i++)
if (isalpha(surname[i]))
res += integerToString(SoundexConvertToDigit(surname[i]));
// remove any consecutive duplicate digits and zeros
for (unsigned int i = 0; i < res.size()-1; i++) {
if (res[i] == res[i+1] || res[i+1] == '0') {
res.erase(i+1, 1);
i--; // there is one less character now
}
}
// if resulting code has length greater than 4, truncate it
if (res.size() > 4)
res.erase(4);
else if (res.size() < 4) // if less than 4, pad with zeros
res.append(4 - res.size(), '0');
return res;
}
int main() {
while (true) {
string surname = getLine("Enter surname (RETURN to quit):");
if (surname == "")
break;
cout << "Soundex code for " << surname << " is " << SoundexCode(surname)
<< endl;
}
return 0;
}