home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 273_01 / soundx.cc < prev    next >
Text File  |  1988-04-18  |  3KB  |  57 lines

  1.  
  2. #include <ctype.h>
  3.  
  4. soundex(char *out_pntr, char *in_pntr)
  5. /*
  6. ┌────────────────────────────────────────────────────────────────────┐
  7. │Purpose: Calculate the soundx code of a string.                     │
  8. │                                                                    │
  9. │ Inputs: char *out_pntr = pointer to a 5 char array to put the      │
  10. │                          soundx code into.                         │
  11. │         char *in_pntr  = pointer to string to calc. soundx code of.│
  12. │                                                                    │
  13. │Outputs: Soundx code stored into area pointed to by *out_pntr.      │
  14. │                                                                    │
  15. │ Return: None                                                       │
  16. └────────────────────────────────────────────────────────────────────┘
  17. */
  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(0);
  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.