Solved

Soundex() Function in C/C++

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

739 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