Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package soundex;
- public class Soundex {
- private String w;
- private char ant;
- private static void main(String[] args) {
- Soundex sdx = new doSoundex();
- String s = new String();
- s = JOptionPane.showInputDialog("Insira o texto:");
- sdx.setW(s);
- s = "Soundex: " + sdx.doSoundex();
- JOptionPane.showMessageDialog(null, s);
- }
- private int checkVogal(char c) {
- if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
- return 1;
- else
- return 0;
- }
- private char replaceSoundex(char c) {
- char d = 0;
- if (c == ant)
- d = '0';
- else {
- if (c == 'b' || c == 'p' || c == 'f' || c == 'v')
- d = '1';
- if (c == 'c' || c == 's' || c == 'g' || c == 'j' || c == 'k' || c == 'q' || c == 'x' || c == 'z')
- d = '2';
- if (c == 'd' || c == 't')
- d = '3';
- if (c == 'l')
- d = '4';
- if (c == 'm' || c == 'n')
- d = '5';
- if (c == 'r')
- d = '6';
- if (c == 'h' || c == 'w' || c == 'y')
- d = '0';
- }
- ant = c;
- return d;
- }
- public void setW(String s) {
- w = s;
- }
- public String Soundex::doSoundex() {
- char temp[] = new char[4];
- int count = 1;
- int i = 0;
- temp[0] = w.charAt(0);
- for (i = 1; i < w.length() || count <= 3; i++) {
- if (checkVogal(w.charAt(i)) == 1) ;
- else {
- temp[count] = replaceSoundex(w.charAt(i));
- count++;
- }
- }
- if (count < 4) {
- for (i = count; i < 4; i++)
- temp[i] = 0;
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement