Finishing Up

What about vowels? Rule #3 also states that otherwise-duplicate encodings separated by a vowel (not h or w) get coded twice.

c2/39/SoundexTest.cpp
 
TEST_F(SoundexEncoding, DoesNotCombineDuplicateEncodingsSeparatedByVowels) {
 
ASSERT_THAT(soundex.encode(​"Jbob"​), Eq(​"J110"​));
 
}

Once again we solve our challenge by declaring what we want to accomplish. We change the conditional expression in encodeLetter to append a digit only if it is not a duplicate or if the last letter is a vowel. This declaration drives a few corresponding changes.

c2/39/Soundex.h
 
void​ encodeTail(std::​string​& encoding, ​const​ std::​string​& word) ​const​ {
*
for​ (​auto​ i = 1u; i < word.length(); i++)
*
if​ (!isComplete(encoding))
*
encodeLetter(encoding, word[i], word[i - 1]);
 
}
 
*
void​ encodeLetter(std::​string​& encoding, ​char​ letter, ​char​ lastLetter) ​const​ {
 
auto​ digit = encodedDigit(letter);
 
if​ (digit != NotADigit &&
*
(digit != lastDigit(encoding) || isVowel(lastLetter)))
 
encoding += digit;
 
}
 
*
bool​ isVowel(​char​ letter) ​const​ {
*
return
*
std::​string​(​"aeiouy"​).find(lower(letter)) != std::​string​::npos;
*
}

Is passing in the previous letter the best way to do this? It’s direct and expressive. We’ll stick with it for now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.233