#include #include using namespace std; string generateSoundex(string st); string convertToUpperCase(string st); int main (int argc, char * const argv[]) { string word1, word2; string soundex1, soundex2; cout << "\n\nNew Horizons Governors School Team B\n"; cout << "Enter the first word: "; cin >> word1; cout << "Enter the second word: "; cin >> word2; word1 = convertToUpperCase(word1); word2 = convertToUpperCase(word2); soundex1 = generateSoundex(word1); soundex2 = generateSoundex(word2); int comparison = soundex1.compare(soundex2); int commonPrefix = 0; for (int i = 0; i < soundex1.length() && i < soundex2.length(); i++) { if (soundex1[i] == soundex2[i]) commonPrefix++; else break; } cout << "\nSoundex of " << word1 << " is " << soundex1 << endl; cout << "Soundex of " << word2 << " is " << soundex2 << endl; if (comparison == 0) cout << "They have the same encoding" << endl; else cout << "They do not have the same encoding" << endl; cout << "They share a common prefix of " << commonPrefix << endl; return 0; } string generateSoundex(string st) { string soundex = st; //discard vowels and vowel-like letters for (int i = soundex.length() - 1; i >= 1; i--) { switch (soundex[i]) { case 'A': case 'E': case 'I': case 'O': case 'U': case 'H': case 'W': case 'Y': soundex.erase(i, 1); break; } } //map letters to digits for (int i = 1; i < soundex.length(); i++) { switch (soundex[i]) { case 'B': case 'P': soundex[i] = '1'; break; case 'F': case 'V': soundex[i] = '2'; break; case 'C': case 'K': case 'S': soundex[i] = '3'; break; case 'G': case 'J': soundex[i] = '4'; break; case 'Q': case 'X': case 'Z': soundex[i] = '5'; break; case 'D': case 'T': soundex[i] = '6'; break; case 'L': soundex[i] = '7'; break; case 'M': case 'N': soundex[i] = '8'; break; case 'R': soundex[i] = '9'; break; } } //discard duplicates for (int i = soundex.length() - 1; i > 1; i--) { if (soundex[i] == soundex[i - 1]) soundex.erase(i, 1); } //and add the initial character st.insert(0, st.substr(0, 1)); return soundex; } string convertToUpperCase(string st) { for (int i = 0; i < st.length(); i++) st[i] = toupper(st[i]); return st; }