Solved

Soundex() Function in C/C++

Posted on 2001-06-29
2
1,474 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 141
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 82
is twain_32.dll cmpatible with windows 10 ? 10 128
C++ question 3 62
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

861 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

22 Experts available now in Live!

Get 1:1 Help Now