public static String soundex(String s) {
    char[] ca = s.toUpperCase().toCharArray();

    StringBuilder res = new StringBuilder();
    char c, prev = '?';

    // Main loop: find up to 4 chars that map.
    for (int i = 0; i < ca.length && res.length() < 4 &&
        (c = ca[i]) != ','; i++) {

      // Check to see if the given character is alphabetic.
      // Text is already converted to uppercase. Algorithm
      // only handles ASCII letters, do NOT use Character.isLetter()!
      // Also, skip double letters.
      if (c >= 'A' && c <= 'Z' && c != prev) {
        prev = c;

        char m = MAP[c - 'A'];
        if (m != '0')
          res.append(m);
      }
    }

    if (res.length() == 0)
      return null;

    for (int i = res.length(); i < 4; i++)
      res.append('0');

    return res.toString();
  }