Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function mySoundex($name) that computes the Soundex code for the given p

ID: 3664833 • Letter: W

Question

Write a function mySoundex($name) that computes the Soundex code for the given parameter name. Soundex is a set of rules that transforms an English name into a hash-code that can be used for similar names (so looking up a name in a database based on the soundex code will yield similar sounding names). The Soundex rules are given at http://en.wikipedia.org/wiki/Soundex. Note: PHP has a built-in soundex() function that you should NOT use for this assignment (the idea is to write it yourself). Test your function using the names "Euler", "Ellery", "Gauss", "Ghosh", "Hilbert", "Heilbronn", "Knuth", "Kant", "Leida", "Ladd", "Lukasiewicz", and "Lissajous"

Explanation / Answer

public static String soundex(String s) {
       char[] x = s.toUpperCase().toCharArray();
       char firstLetter = x[0];

       // convert letters to numeric code
       for (int i = 0; i < x.length; i++) {
           switch (x[i]) {

           case 'B':
           case 'F':
           case 'P':
           case 'V':
               x[i] = '1';
               break;

           case 'C':
           case 'G':
           case 'J':
           case 'K':
           case 'Q':
           case 'S':
           case 'X':
           case 'Z':
               x[i] = '2';
               break;

           case 'D':
           case 'T':
               x[i] = '3';
               break;

           case 'L':
               x[i] = '4';
               break;

           case 'M':
           case 'N':
               x[i] = '5';
               break;

           case 'R':
               x[i] = '6';
               break;

           default:
               x[i] = '0';
               break;
           }
       }

       // remove duplicates
       String output = "" + firstLetter;
       for (int i = 1; i < x.length; i++)
           if (x[i] != x[i - 1] && x[i] != '0')
               output += x[i];

       // pad with 0's or truncate
       output = output + "0000";
       return output.substring(0, 4);
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote