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