Soundex (sounds like)

William G. Hutchison wgh at ubbpc.UUCP
Wed Dec 13 06:30:15 AEST 1989


In article <488 at hades.OZ>, ing at hades.OZ (Ian Gold) writes:
> 
> I am looking for a 'soundex' routine in C (or C++).  

/* soundex.c - Generate a soundex code from a character string		*/

/*	Odell, Margaret K. and Russell, Robert C.			*/
/*	U. S. Patents 1261167 (1918) and 1435663 (1922)			*/

/* Implemented in C by William Hutchison, Data Processing Consultant	*/
/* 					  Unisys Corporation		*/
/* Copyright 1985 William G. Hutchison, Jr.				*/

/* see also:								*/
/*	Knuth, Donald E. "The Art of Computer Programming", vol 3.	*/
/* Searching and Sorting, p. 391-92, Addison-Wesley, Reading, MA, 1973	*/

/* to compile: cc -o soundex -DMAIN soundex.c				*/


#include <stdio.h>
#include <string.h>
#include <assert.h>

char	*progname;


char*
soundex(s, code)
char	*s;				/* input character string	*/
char	*code;				/* output soundex code string	*/
{
#ifndef lint
	static char	Vers[] = 
	"@(#)soundex.c	1.2  Compiled: 14:25:54 12/12/89  Delta Date: 14:25:49 12/12/89";
#endif
	char* 	p = code;
	int	chars_needed = 3;
	static char
	code_table[] = "B\1F\1P\1V\1C\2G\2J\2K\2Q\2S\2X\2Z\2D\3T\3L\4M\5N\5R\6";
	char*	x;
#define	encode(c) (x=strchr(code_table,(c)),(x == NULL)?'\0':*++x + '0')
	char	last_code = encode(*s);

	assert(s != NULL);
	assert(code != NULL);
	assert(strlen(code) >= 4);

	*p++ = toupper(*s++);		/* copy first character as is	*/

	while (*s && chars_needed) {
		char	c = toupper(*s++);
		char	new_code;

		if ((new_code = encode(c)) && new_code != last_code) {
			last_code = *p++ = new_code;
			chars_needed--;
		} else
			last_code = ' ';
	}

	while (chars_needed-- > 0)
		*p++ = '0';
	*p = '\0';

	return code;
}					/* end soundex() */


#ifdef MAIN

main(argc, argv)
int	argc;
char	*argv[];
{
	char	string[BUFSIZ];
	static char	code[] = "K123";

	progname = argv[0];

	while (gets(string)) {
		puts(soundex(string, code));
	}

	return 0;
}				/* end main */

/* 
 	Sample input test stream:

Euler
Ellery
Gauss
Ghosh
Hilbert
Heilbronn
Hutcherson
Hutchison
Hutchinson
Knuth
Kant
Lloyd
Ladd
Lukasiewicz
Lissajous

	Sample output from the above input:

E460
E460
G200
G200
H416
H416
H326
H322
H325
K530
K530
L300
L300
L222
L222

 */

#endif

/* EOF soundex.c */
-- 
Bill Hutchison, DP Consultant	rutgers!cbmvax!burdvax!ubbpc!wgh
Unisys UNIX Portation Center	"Unless you are very rich and very eccentric,
P.O. Box 500, M.S. B121         you will not enjoy the luxury of a computer
Blue Bell, PA 19424		in your own home", Edward Yourdon, 1975.



More information about the Comp.lang.c mailing list