File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Soundex {
2
+
3
+ public static void soundex (String str ) {
4
+
5
+ char [] s = str .toUpperCase ().toCharArray ();
6
+ //retaining first letter
7
+ String output = s [0 ] + "" ;
8
+
9
+ //replacing consonants with digits
10
+ for (int i = 0 ; i <s .length ; i ++) {
11
+ switch (s [i ]) {
12
+ case 'B' :
13
+ case 'F' :
14
+ case 'P' :
15
+ case 'V' :
16
+ s [i ] = '1' ;
17
+ break ;
18
+
19
+ case 'C' :
20
+ case 'G' :
21
+ case 'J' :
22
+ case 'K' :
23
+ case 'Q' :
24
+ case 'S' :
25
+ case 'X' :
26
+ case 'Z' :
27
+ s [i ] = '2' ;
28
+ break ;
29
+
30
+ case 'D' :
31
+ case 'T' :
32
+ s [i ] = '3' ;
33
+ break ;
34
+
35
+ case 'L' :
36
+ s [i ] = '4' ;
37
+ break ;
38
+
39
+ case 'M' :
40
+ case 'N' :
41
+ s [i ] = '5' ;
42
+ break ;
43
+
44
+ case 'R' :
45
+ s [i ] = '6' ;
46
+ break ;
47
+
48
+ default :
49
+ s [i ] = '0' ;
50
+ break ;
51
+
52
+ }
53
+ }
54
+
55
+ // removing duplicates
56
+ for (int i = 1 ; i < s .length ; i ++) {
57
+ if (s [i ] != s [i -1 ] && s [i ] != '0' )
58
+ output += s [i ];
59
+ }
60
+
61
+ // right padding with zeroes or truncating
62
+ output = output + "0000" ;
63
+ System .out .println (str + " : " + output .substring (0 , 4 ));
64
+ }
65
+
66
+ public static void main (String [] args ) {
67
+
68
+ String str1 = "Dear" ;
69
+ String str2 = "Deer" ;
70
+ String str3 = "Ashcraft" ;
71
+ String str4 = "Ashcroft" ;
72
+ soundex (str1 );
73
+ soundex (str2 );
74
+ soundex (str3 );
75
+ soundex (str4 );
76
+ }
77
+ }
You can’t perform that action at this time.
0 commit comments