Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Soundex() Function in C/C++

Posted on 2001-06-29
2
Medium Priority
?
1,614 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 200 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

670 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