Solved

Soundex() Function in C/C++

Posted on 2001-06-29
2
1,457 Views
Last Modified: 2008-01-09
looking for source code listing in C/C++ that returns a phonetic representation of a specified Character expresssion.
0
Comment
Question by:Loysi
2 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6240302
see http://www.openhere.com/science/computer-science/algorithms/soundex-code/
soundex resources and
http://www.snippets.org/ soundex code of Bob Stout
same thing (mirror)
http://www.brokersys.com/snippets/
files Phonetic.H,Soundex4.C,Soundex5.C,Metaphon.C,Approx.C
Alex
0
 
LVL 1

Accepted Solution

by:
smnaha earned 50 total points
ID: 6240398
Hi Alex,

This is the soundex algorithm implementation which I use. You might want to validate the input data more rigorously than what it is here. It is an implementation of the algorithm described in Knuth's The Art of Computer Programming, Volume 3.

-smnaha
-----------------------------------------------------------
#include <stdlib.h>
#include <string.h>


const  int CODE_LEN = 4;

static char soundnum[26] = {
     '0', '1', '2', '3', '0', '1', '2', 'h', '0','2',
     '2', '4', '5', '5', '0', '1', '2', '6', '2','3',
     '0', '1', 'w', '2', '0', '2'};

int compact (char *s, int nsize)
{
     int i = 0, j = 0;
     
     for (i=1, j=1; i < nsize; ++i) {
          if (s[i] != s[i-1]) {    
               s[j] = s[i];
                      ++j;
          }
     }
     s[j] = '\0';
     
     return j;
}

/* soundex code for a string is always 4 characters in length */
/* this soundex code is the implementation of the soundex */
/* algorithm described in "The Art of Computer Programming, Vol 3, */
/* Sorting and Searching, by Donald E. Knuth */
int __stdcall GetSoundCode (const char *szString, char *szCode)
{
     char *cp;
     char *stemp ;
     char *s = 0;
     int len = 0;

     if (szCode == 0) {
          return 0;
     }

     len = strlen(szCode);
     if (len < CODE_LEN)
          return 0;

     stemp = (char*)malloc(strlen(szString) * sizeof(char));
     if (stemp == 0)
          return 0;

     strcpy (stemp, szString);

     s = stemp;
     ++s;
     while (*s) {
          *s = soundnum[*s - 'a'];
          if (*s == 'h' || *s == 'w')
               *s = *(s-1);
          ++s;
     }
     compact (stemp, strlen(stemp));
     szCode[0] = szCode[1] = szCode[2] = szCode[3] = '0';
     szCode[4] = '\0';
     cp = szCode;
     s = stemp;
     while (*s && *cp) {
          if (*s != '0') {
               *cp = *s;
               ++cp;
          }
          ++s;
     }

     return 1;
}
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now