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, ... |
Get Modern C++ Programming with Test-Driven Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.