home *** CD-ROM | disk | FTP | other *** search
- /*********
- * SOUNDEX
- *
- * by Leonard Zerman and Tom Rettig
- *
- * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
- *
- * Syntax: SOUNDEX( <expC> )
- * Return: A code as a four char string in the form of A999.
- * Note : This is based upon the Soundex algorithm in
- * Donald E. Knuth from The Art of Computer Programming,
- * Volume 3, "Sorting and Searching", page 392.
- *********/
-
- #include "trlib.h"
-
- TRTYPE soundex() /* declare the soundex function */
- {
- char *instr; /* a pointer to the passed string */
- static char funcname[] = {"soundex"}; /* program name for errors */
- static char ret[5], hold[1];
- int i;
-
- if (PCOUNT==1 && ISCHAR(1) ) /* if there is one parameter and */
- { /* it is a char variable */
- instr = _parc(1); /* assign the address to instr */
- }
- else
- {
- _retc(_tr_errmsgs(funcname, E_SYNTAX)); /* return an error */
- return; /* return from program */
- }
-
- /* first char of string must be alpha ::= first char of soundex code */
- if ( isalpha(*instr) )
- {
- ret[0] = toupper(*instr);
- hold[0] = ret[0];
- }
- else
- { /* return blank string to */
- ret[0] = SPACEC; /* place at top of index */
- ret[1] = SPACEC;
- ret[2] = SPACEC;
- ret[3] = SPACEC;
- ret[4] = NULLC;
- _retc(ret);
- return;
- }
-
- i = 1;
- instr++;
- while (*instr && i <= 3)
- {
- if ( hold[0] == toupper(*instr) )
- { /* skip repeating characters */
- instr++;
- continue;
- }
-
- switch( *instr ) /* if it is one of these choices, */
- { /* put the proper code into ret[] */
- case 'b':
- case 'B':
- case 'f':
- case 'F':
- case 'p':
- case 'P':
- case 'v':
- case 'V':
- hold[0] = toupper(*instr);
- ret[i++] = '1';
- break;
- case 'c':
- case 'C':
- case 'g':
- case 'G':
- case 'j':
- case 'J':
- case 'k':
- case 'K':
- case 'q':
- case 'Q':
- case 's':
- case 'S':
- case 'x':
- case 'X':
- case 'z':
- case 'Z':
- hold[0] = toupper(*instr);
- ret[i++] = '2';
- break;
- case 'd':
- case 'D':
- case 't':
- case 'T':
- hold[0] = toupper(*instr);
- ret[i++] = '3';
- break;
- case 'l':
- case 'L':
- hold[0] = toupper(*instr);
- ret[i++] = '4';
- break;
- case 'm':
- case 'M':
- case 'n':
- case 'N':
- hold[0] = toupper(*instr);
- ret[i++] = '5';
- break;
- case 'r':
- case 'R':
- hold[0] = toupper(*instr);
- ret[i++] = '6';
- break;
- }
- instr++;
- }
-
- /* if the soundex code is less than four, fill with '0' */
- while (i <= 3)
- ret[i++] = '0';
-
- ret[i] = NULLC; /* null terminator */
-
- _retc(ret); /* return soundex string */
- }
- /* eof soundex */
-
-