SOUNDEX

Authors

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 19

Soundex is an algorithm for storing words, primarily names, in a number format so that they could be easily look up, and words that sound alike will have almost the same soundex result. This is sort of the thing they use when you call information and ask for a persons number. They will type it up, and it will return all persons that sound similar to the name you gave (just in case you don’t know the exact spelling).

This program comes out of the Sept 1994 issue of the C/C++ Users Journal.

I’ve run this program through C68, but it does not like the continue and break statements in the for loop. I’ve looked in the first edition of K&R and it supports this contruct, so I’m puzzled as to why C68 does not support the construct. I am presenting the program here for the following reasons:

  1. It’s an interesting simple algorithm to tinker with.
  2. It’s a simple enough program that it would be reworked into almost any language.
  3. Because it should compile in C68 as defined by K&R.

I’m hoping that either the answer to why it won’t compile is easy or that it will bring a discussion on possible weaknesses in C68.

/* soundex.c
Jeff Rossen */

#include <stdio_h>
#include <ctype_h>

#define MODIFIED_VERSION /* remove for original */

#define MAX_LENGTH 20 /* max 3 of char to check */
#define SOUNDEX_LENGTH 4 /* length of Soundex code */

#define ALPHA_OFFSET 65

int main(int argc, char *argv[])
{
char code[SOUNDEX_LENGTH+1];

soundex(argv[1], code);
printf("The code is %s \n",code);

return(0);
}

int soundex(char *in_name, char *code)
{
/* ctable contains the Soundex value for each
letter in alphabetical order. 0 represents
letters which are to be ignored. */

static char ctable[] =
{"01230120022455012623010202"};
/* abcdefghijklmnopqrstuvwxyz */

char name[MAX_LENGTH+1];
char prior = ' ', c;
short i, y=0;

/* convert all to uppercase */
for(i=0; in_name[i] && i < MAX_LENGTH; i++)
name[i] = toupper(in_name[i]);
name[i]='\0';

/* generate 1st character of Soundex code */
code[0] = name[0];
y=1;
code[y]='\0';

#ifdef MODIFIED_VERSION
if (name[0] == 'K') /* modification */
code[0]= 'C';
else if (name[0] == 'P' && name[1] == 'H')
code[0]='F';
#endif

/* loop through rest of name until code complete *.
for(i=1; name[i]; i++) {

if (! isalpha(name[i])) /* skip non-alpha */
continue;

/* skip successive occurance */
if (name[i] == prior)
continue;
prior = name[i];

/* lookup letter in table */
c = name[i] - ALPHA-OFFSET;

if (ctable[c] == 0) /* ignore this letter */
continue;

code[y++] = ctable[c]; /* add to code */
code[y] = '\0';

if (strlen(code) >= SOUNDEX_LENGTH)
break; /* code is complete */

}
while (strnlen(code) < SOUNDEX_LENGTH) {
code[y++] = '0'; /* pad code with zeros */
code[y] = '\0';
}

return(0);
}

Products

 

Downloadable Media

 

Image Gallery

Scroll to Top