Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Soundex/Java/soundex.java
Normal file
44
Task/Soundex/Java/soundex.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
public static void main(String[] args){
|
||||
System.out.println(soundex("Soundex"));
|
||||
System.out.println(soundex("Example"));
|
||||
System.out.println(soundex("Sownteks"));
|
||||
System.out.println(soundex("Ekzampul"));
|
||||
}
|
||||
|
||||
private static String getCode(char c){
|
||||
switch(c){
|
||||
case 'B': case 'F': case 'P': case 'V':
|
||||
return "1";
|
||||
case 'C': case 'G': case 'J': case 'K':
|
||||
case 'Q': case 'S': case 'X': case 'Z':
|
||||
return "2";
|
||||
case 'D': case 'T':
|
||||
return "3";
|
||||
case 'L':
|
||||
return "4";
|
||||
case 'M': case 'N':
|
||||
return "5";
|
||||
case 'R':
|
||||
return "6";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String soundex(String s){
|
||||
String code, previous, soundex;
|
||||
code = s.toUpperCase().charAt(0) + "";
|
||||
|
||||
// EDITED : previous = "7";
|
||||
previous = getCode(s.toUpperCase().charAt(0));
|
||||
|
||||
for(int i = 1;i < s.length();i++){
|
||||
String current = getCode(s.toUpperCase().charAt(i));
|
||||
if(current.length() > 0 && !current.equals(previous)){
|
||||
code = code + current;
|
||||
}
|
||||
previous = current;
|
||||
}
|
||||
soundex = (code + "0000").substring(0, 4);
|
||||
return soundex;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue