home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 14 / CD_ASCQ_14_0694.iso / maj / 653 / soundex.c < prev    next >
C/C++ Source or Header  |  1994-04-03  |  1KB  |  44 lines

  1. /*
  2. ** from Bob Jarvis
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7.  
  8. char *soundex(char *instr, char *outstr)
  9. {                   /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
  10.         char *table = "01230120022455012623010202";
  11.         int count = 0;
  12.  
  13.         while(!isalpha(instr[0]) && instr[0])
  14.                 ++instr;
  15.  
  16.         if(!instr[0])     /* Hey!  Where'd the string go? */
  17.                 return(NULL);
  18.  
  19.         if(toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H')
  20.         {
  21.                 instr[0] = 'F';
  22.                 instr[1] = 'A';
  23.         }
  24.  
  25.         *outstr++ = (char)toupper(*instr++);
  26.  
  27.         while(*instr && count < 5)
  28.         {
  29.                 if(isalpha(*instr) && *instr != *(instr-1))
  30.                 {
  31.                         *outstr = table[toupper(instr[0]) - 'A'];
  32.                         if(*outstr != '0')
  33.                         {
  34.                                 ++outstr;
  35.                                 ++count;
  36.                         }
  37.                 }
  38.                 ++instr;
  39.         }
  40.  
  41.         *outstr = '\0';
  42.         return(outstr);
  43. }
  44.