Solved

Soundex() Function in C/C++

Posted on 2001-06-29
2
1,590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

632 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