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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** Public domain from Bob Jarvis
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "phonetic.h"
  10.  
  11. char *soundex(char *instr, char *outstr)
  12. {                   /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
  13.         char *table = "01230120022455012623010202";
  14.         char *outptr = outstr;
  15.         int count = 0;
  16.  
  17.         while(!isalpha(instr[0]) && instr[0])
  18.                 ++instr;
  19.  
  20.         if(!instr[0])     /* Hey!  Where'd the string go? */
  21.                 return(NULL);
  22.  
  23.         if(toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H')
  24.         {
  25.                 instr[0] = 'F';
  26.                 instr[1] = 'A';
  27.         }
  28.  
  29.         *outptr++ = (char)toupper(*instr++);
  30.  
  31.         while(*instr && count < 5)
  32.         {
  33.                 if(isalpha(*instr) && *instr != *(instr-1))
  34.                 {
  35.                         *outptr = table[toupper(instr[0]) - 'A'];
  36.                         if(*outptr != '0')
  37.                         {
  38.                                 ++outptr;
  39.                                 ++count;
  40.                         }
  41.                 }
  42.                 ++instr;
  43.         }
  44.  
  45.         *outptr = '\0';
  46.         return(outstr);
  47. }
  48.  
  49. #ifdef TEST
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. main(int argc, char *argv[])
  55. {
  56.       char code[6];
  57.  
  58.       if (argc != 2)
  59.       {
  60.             puts("Usage: SOUNDEX string");
  61.             return EXIT_FAILURE;
  62.       }
  63.  
  64.       printf("soundex(\"%s\") returned %s\n",
  65.             argv[1], soundex(argv[1], code));
  66.  
  67.       return EXIT_SUCCESS;
  68. }
  69.  
  70. #endif /* TEST */
  71.