home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / soundex1.arc / SOUNDEX.C
Text File  |  1986-05-19  |  2KB  |  80 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. char *in_pntr;
  17. char *out_pntr;
  18. {
  19. extern char get_scode();
  20. char ch,last_ch;
  21. int count = 0;
  22.  
  23.     strcpy(out_pntr,"0000");    /* Pre-fill output string for    */
  24.                     /* error and trailing zeros.     */
  25.     *out_pntr = toupper(*in_pntr);     /* Copy first letter             */
  26.     last_ch = get_scode(*in_pntr);    /* code of the first letter      */
  27.                     /* for the first 'double-letter  */
  28.                     /* check.             */
  29.                     /* Loop on input letters until   */
  30.                     /* end of input (null) or output */
  31.                     /* letter code count = 3     */
  32.  
  33.     while( (ch = get_scode(*(++in_pntr)) ) && (count < 3) )
  34.     {
  35.     if( (ch != '0') && (ch != last_ch) ) /* if not skipped or double */
  36.         *(out_pntr+(++count)) = ch; /* letter, copy to output */
  37.         last_ch = ch;    /* save code of last input letter for */
  38.                 /* next double-letter check */
  39.     }
  40.     return(out_pntr);    /* pointer to input string */
  41. }
  42.  
  43. char get_scode(ch)
  44. char ch;
  45. {
  46.                 /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
  47.                     /* :::::::::::::::::::::::::: */
  48. static char soundex_map[] =   "01230120022455012623010202";
  49.  
  50.     /* If alpha, map input letter to soundex code. If not, return 0 */
  51.  
  52.     if( !isalpha(ch) )    /*error if not alpha */
  53.         return(0);
  54.     else
  55.         return(soundex_map[(toupper(ch) - 'A')] );
  56. }
  57. #include <ctype.h>
  58.  
  59. main(argc, argv)
  60. int    argc;
  61. char    *argv[];
  62. {
  63. char    *code[10];
  64.  
  65. int    i;
  66.  
  67.     if(argc == 1) /* No arguments, give usage */
  68.     {
  69.     printf("\nUsage: soundex (name) (...)\n");
  70.     exit(1);
  71.     }
  72.  
  73.     for(i = 1; i < argc; i++)
  74.     {
  75.     soundex(code, argv[i]) ;
  76.     printf("The Soundex Code for \"%s\" is: %s\n", argv[i],code);
  77.     }
  78.     exit(0);
  79. }
  80.