Soundex

public class Soundex { static void soundex(String inputString) { char last = 0; byte code; int i, small, len; char[] soundex_table = {0, /* A */ '1', /* B */ '2', /* C */ '3', /* D */ 0, /* E */ '1', /* F */ '2', /* G */ 0, /* H */ '3', /* I */ '2', /* J */ '2', /* K */ '4', /* L */ '5', /* M */ '5', /* N */ 0, /* O */ '1', /* P */ '2', /* Q */ '6', /* R */ '2', /* S */ '3', /* T */ 0, /* U */ '1', /* V */ 0, /* W */ '2', /* X */ 0, /* Y */ '2'}; /* Z */ len = inputString.length(); /* build soundex string */ System.out.println(inputString); byte[] bytearray = inputString.toUpperCase().getBytes(); byte[] soundex = new byte[4]; for (i = 0, small = 0; i < len && small < 4; i++) { /* convert chars to upper case and strip non-letter chars */ /* BUG: should also map here accented letters used in non */ /* English words or names (also found in English text!): */ /* esstsett, thorn, n-tilde, c-cedilla, s-caron, ... */ code = bytearray[i]; if (code >= 'A' && code <= 'Z') { if (small == 0) { /* remember first valid char */ soundex[small++] = code; last = soundex_table[code - 'A']; System.out.println(last); } else { /* ignore sequences of consonants with same soundex */ /* code in trail, and vowels unless they separate */ /* consonant letters */ code = (byte) soundex_table[code - 'A']; if (code != last) { if (code != 0) { soundex[small++] = code; } last = (char) code; //System.out.println(code +"-"+last); } } } } /* pad with '0' and terminate with 0 ;-) */ while (small < 4) { soundex[small++] = '0'; } String resultstr = new String(soundex); System.out.println(resultstr); } public static void main(String[] args) { // // gets a connection to an access server // if (args.length < 1) { System.err.println("Usage: java Soundex string"); System.exit(1); } String woord = args[0]; soundex(woord); } }