Skip to content

Instantly share code, notes, and snippets.

@Fabccc
Created April 11, 2019 06:00
Show Gist options
  • Save Fabccc/815149b1e424e0a73f18d9cdb00ca478 to your computer and use it in GitHub Desktop.
Save Fabccc/815149b1e424e0a73f18d9cdb00ca478 to your computer and use it in GitHub Desktop.
public static String getSoundexCode(String s){
char[] x = s.toUpperCase().toCharArray();
char firstLetter = x[0];
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;
}
}
}
String output = "" + firstLetter;
for (int i = 1; i < x.length; i++)
if (x[i] != x[i - 1] && x[i] != '0')
output += x[i];
output+="0000";
return output.substring(0, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment