home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SOUNDEX5.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  99 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  S O U N D E X 5
  5. **
  6. **  Originally from SNIPPETS, written by Bob Jarvis.
  7. **  Modified by M. Stapleton of Graphic Bits on Dec  8 1994
  8. **  Uses compressed heptal - eliminates codes for non-trailing zeros.
  9. */
  10.  
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include "phonetic.h"
  14.  
  15. int soundex5(char *instr)
  16. {
  17.       int count = 0, init, snd = 0, notlast = 1;
  18.       int ch;
  19.       static int table[] =
  20.       {
  21.       /* A  B  C  D  E     F  G  H  I  J */
  22.          0, 1, 2, 3, 0,    1, 2, 0, 0, 2,
  23.  
  24.       /* K  L  M  N  O     P  Q  R  S  T */
  25.          2, 4, 5, 5, 0,    1, 2, 6, 2, 3,
  26.  
  27.       /* U  V  W  X  Y  Z */
  28.          0, 1, 0, 2, 0, 2,
  29.       };
  30.  
  31.       /* Skip leading non-alpha */
  32.  
  33.       while(*instr && (!isalpha(*instr)))
  34.             instr++;
  35.  
  36.       if(!*instr)     /* Hey!  Where'd the string go? */
  37.             return -1;
  38.  
  39.       /* Convert initial letter to int. */
  40.  
  41.       init = (char)toupper(instr[0]) - 'A';
  42.  
  43.       for(instr++; *instr && (count < 3); instr++)
  44.       {
  45.             if(isalpha(*instr) && (*instr != *(instr-1)))
  46.             {
  47.                   if(NUL != (ch = table[toupper(*instr) - 'A']))
  48.                   {
  49.                         /* Convert to "compressed heptal" */
  50.  
  51.                         snd = (7 - notlast) * snd + ch - notlast;
  52.                         notlast = ++count < 2;
  53.                   }
  54.             }
  55.       }
  56.  
  57.       /* Adjust */
  58.  
  59.       switch(count)
  60.       {
  61.       case 0:     /* default:    Shouldn't get here! */
  62.             snd = 0;
  63.             break;
  64.  
  65.       case 1:
  66.             snd += 1;
  67.             break;
  68.  
  69.       case 2:
  70.             snd *= 7;
  71.             /* Fall through */
  72.  
  73.       case 3:
  74.             snd += 7;
  75.             break;
  76.       }
  77.       return SNDMAX * init + snd;
  78. }
  79.  
  80. #ifdef TEST
  81.  
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84.  
  85. main(int argc, char *argv[])
  86. {
  87.       if (argc != 2)
  88.       {
  89.             puts("Usage: SOUNDEX5 string");
  90.             return EXIT_FAILURE;
  91.       }
  92.  
  93.       printf("soundex5(\"%s\") returned %d\n", argv[1], soundex5(argv[1]));
  94.  
  95.       return EXIT_SUCCESS;
  96. }
  97.  
  98. #endif /* TEST */
  99.