home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / STRINDS.ZIP / SOUNDEX.C < prev    next >
Encoding:
Text File  |  1987-02-23  |  2.4 KB  |  106 lines

  1. /****************************************************************
  2. *    SOUNDEX ALGORITHM in C                    *
  3. *                                 *
  4. *    The basic Algorithm source is taken from EDN Nov.    *
  5. *    14, 1985 pg. 36.                    *
  6. *                                *
  7. *    As a test Those in Illinois will find that the        *
  8. *    first group of numbers in their drivers license        *
  9. *    number is the soundex number for their last name.    *
  10. *                                *
  11. *    RHW  PC-IBBS ID. #1230                    *
  12. *                                *
  13. ****************************************************************/
  14.  
  15. char (*soundex(out_pntr, in_pntr))
  16.  
  17. char *in_pntr;
  18.  
  19. char *out_pntr;
  20.  
  21.   {
  22.     extern char get_scode();
  23.  
  24.     char ch,last_ch;
  25.  
  26.     int count = 0;
  27.  
  28.     strcpy(out_pntr,"0000");    /* Pre-fill output string for    */
  29.                     /* error and trailing zeros.     */
  30.     *out_pntr = toupper(*in_pntr);     /* Copy first letter             */
  31.  
  32.     last_ch = get_scode(*in_pntr);    /* code of the first letter      */
  33.                     /* for the first 'double-letter  */
  34.                     /* check.             */
  35.                     /* Loop on input letters until   */
  36.                     /* end of input (null) or output */
  37.                     /* letter code count = 3     */
  38.  
  39.     while( (ch = get_scode(*(++in_pntr)) ) && (count < 3) )
  40.  
  41.       {
  42.     if ( (ch != '0') && (ch != last_ch) ) /* if not skipped or double */
  43.  
  44.           *(out_pntr+(++count)) = ch; /* letter, copy to output */
  45.     
  46.         last_ch = ch;    /* save code of last input letter for */
  47.             /* next double-letter check */
  48.       }
  49.  
  50.     return(out_pntr);    /* pointer to input string */
  51.   }
  52.  
  53.  
  54. char get_scode(ch)
  55.  
  56. char ch;
  57.  
  58.   { 
  59.                                 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
  60.                                 /* :::::::::::::::::::::::::: */
  61.     static char soundex_map[] =   "01230120022455012623010202";
  62.  
  63. /* If alpha, map input letter to soundex code. If not, return 0 */
  64.  
  65.     if( !isalpha(ch) )    /*error if not alpha */
  66.  
  67.       return(0);
  68.  
  69.     else
  70.  
  71.       return(soundex_map[(toupper(ch) - 'A')] );
  72.   }
  73.  
  74. #include <ctype.h>
  75.  
  76. main(argc, argv)
  77.  
  78. int    argc;
  79.  
  80. char    *argv[];
  81.  
  82.   {
  83.     char *code[10];
  84.  
  85.     int i;
  86.  
  87.     if (argc == 1) /* No arguments, give usage */
  88.  
  89.       {
  90.     printf("\nUsage: soundex (name) (...)\n");
  91.  
  92.     exit(1);
  93.  
  94.       }
  95.  
  96.     for(i = 1; i < argc; i++)
  97.  
  98.       {
  99.     soundex(code, argv[i]) ;
  100.  
  101.     printf("The Soundex Code for \"%s\" is: %s\n", argv[i],code);
  102.       }
  103.  
  104.     exit(0);
  105.   }
  106.