You are on page 1of 1
// Remove the vowels from the encoding. encoding = encoding.Replace("0", ""); // Pad or truncate if necessary Af (encoding-Length < 3) encoding = encoding.PadRight (3, °0"); else if (encoding-Length > 3) encoding = encoding. Substring (0, 3); // Add the original nane's letter at the beginning and return. return name[0] + encoding; , This code begins by defining the Let terCodes string, That string contains digits to encode cach of the letters, from A through Z. For example, D is the fourth letter in the alphabet and the tet terCodes string's fourth character is 3, which is the encoding for D. Vowels, H, and Ware encoded as 0. The ToSoundex method returns if its input string is blank. If the name is not blank, it converts the name into uppercase and removes any H or W characters that come after the name's first letter. Next, the code loops through the name's remaining characters. It subtracts A from each character to find the character's position in the alphabet and uses that as an index into the Let terCodes string, If the current letter’s encoding is different from the preceding letter's encoding, the method adds it to the name's encoding, ‘The code then removes the first letter from the encoding because that letter will be represented by the original name's first letter rather than a number. The code removes the vowel codes, which are Os, from the rest of the encoding, and pads or truncates the encoding so that it contains three digits. Finally, the code adds the name's first letter to the beginning of the encoding and returns, the result. Download the Soundex example solution to see additional details. 63. Longest common substring The following code uses the obvious approach described in the problem statement that examines all of the first string’s possible substrings: // Find the longest subst // substring in string! ng by examining every possible

You might also like