home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / utils / soundex1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  3.3 KB  |  122 lines

  1. Article 7 of alt.sources:
  2. Path: mit-eddie!husc6!ncsuvx!lll-winken!ohlone!lll-tis!ptsfa!jmc
  3. From: jmc@ptsfa.UUCP (Jerry Carlin)
  4. Newsgroups: alt.sources
  5. Subject: Soundex
  6. Date: 9 Jul 87 21:02:16 GMT
  7.  
  8. Here is a short program I wrote a few years ago illustrating the use
  9. of the "soundex" algorithm. It is used to generate the same token for
  10. similarly pronounced names (like Smith and Smythe).
  11.  
  12. /* soundex algorithm from 1918 patent */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16.  
  17. #define MAXNAME 30      /* max name length */
  18. #define SOUNDLEN 6      /* max len soundex string */
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.         char name[MAXNAME];     /* input string for surname */
  25.         char sound[SOUNDLEN];   /* SOUNDLEN -1 length */
  26.         int namelen;            /* length of entered name */
  27.         int i;
  28.  
  29.         if (argc > 1)
  30.         {
  31.                 for (i = 1; i < argc; i++)
  32.                 {
  33.                         getsound(argv[i],sound);
  34.                         printf("%s\n",sound);
  35.                 }
  36.                 exit(0);
  37.         }
  38.         gets(name);
  39.         if (name[0] == '\0')
  40.                 exit(0);
  41.         getsound(name,sound);
  42.         printf("%s\n",sound);
  43.         exit(0);
  44. }
  45.  
  46. /***************************************************************************/
  47. /* soundex algorhithm - format of result is annnn                          */
  48. /***************************************************************************/
  49.  
  50. getsound(name,sound)
  51. char *name;
  52. char *sound;
  53. {
  54.         int i;
  55.         int j;
  56.         int val;        /* char value */
  57.         int oldval;
  58.         char tempc;
  59.         int namelen;
  60.  
  61.         namelen = strlen(name);
  62.         j = 1;          /* start 2nd pos */
  63.         oldval = 0;
  64.         tempc = name[0];
  65.         sound[0] = isupper(tempc) ? tolower(tempc) : tempc;
  66.         for (i = 1; (i <= namelen) && (j < SOUNDLEN - 1); i++)
  67.         {
  68.                 tempc = name[i];
  69.                 if (isupper(tempc))
  70.                         tempc = tolower(tempc);
  71.                 switch (tempc)
  72.                 {
  73.                 case 'b':
  74.                 case 'f':
  75.                 case 'p':
  76.                 case 'v':
  77.                         val = 1;
  78.                         break;
  79.                 case 'c':
  80.                 case 'g':
  81.                 case 'j':
  82.                 case 'k':
  83.                 case 'q':
  84.                 case 's':
  85.                 case 'x':
  86.                 case 'z':
  87.                         val = 2;
  88.                         break;
  89.                 case 'd':
  90.                 case 't':
  91.                         val = 3;
  92.                         break;
  93.                 case 'l':
  94.                         val = 4;
  95.                         break;
  96.                 case 'm':
  97.                 case 'n':
  98.                         val = 5;
  99.                         break;
  100.                 case 'r':
  101.                         val = 6;
  102.                         break;
  103.                 default:
  104.  
  105.                         val = 0;
  106.                         break;
  107.                 }
  108.                 if (val != 0 && val != oldval)
  109.                         sound[j++] = val + '0';
  110.                 oldval = val;
  111.         }
  112.         while (j < SOUNDLEN-1)
  113.                 sound[j++] = '0';
  114.         sound[SOUNDLEN] = '\0';
  115. }
  116.  
  117. -- 
  118. voice: (415) 823-2441    uucp: {ihnp4,lll-crg,ames,qantel,pyramid}!ptsfa!jmc
  119. Where am I? In the village. Whose side are you on? That would be telling.
  120.  
  121.  
  122.