Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function mySoundex($name) that computes the Soundex code for the given p

ID: 3669812 • Letter: W

Question

Write a function mySoundex($name) that computes the Soundex code for the given parameter name. Soundex is a set of rules that transforms an English name into a hash-code that can be used for similar names (so looking up a name in a database based on the soundex code will yield similar sounding names). The Soundex rules are given at http://en.wikipedia.org/wiki/Soundex. Note: PHP has a built-in soundex() function that you should NOT use for this assignment (the idea is to write it yourself). Test your function using the names "Euler", "Ellery", "Gauss", "Ghosh", "Hilbert", "Heilbronn", "Knuth", "Kant", "Leida", "Ladd", "Lukasiewicz", and "Lissajous"

Explanation / Answer


//program for Soundex code for the given parameter name.
//-f treats the first character of name just like the others.
//If no names are specified in the arguments, mySoundex will read
//from the standard input until end of file is encountered.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXNAMELEN 50
#define MAXLINELEN 500

int FullFlag;

//Deletes the first word from a string
char *delfirstword(char *s)
{
char *sout = (char *)malloc(strlen(s));
int i = 0, i2 = 0;

if (s[0] == (char)NULL) return s;
while (isspace(s[i])) i++;
while ((!isspace(s[i])) && (s[i] != (char)NULL)) i++;
if (s[i] == (char)NULL) return "";
while ((isspace(s[i])) && (s[i] != (char)NULL)) i++;
while (s[i] != (char)NULL)
{
sout[i2] = s[i];
i++; i2++;
}
sout[i2] = (char)NULL;
return sout;
}

//Returns the mySoundex equivalent to In
char *MySoundex(char *In)
{
int Iin, Iout;
char C, PrevDig, *Out = (char *)malloc(10);

if (FullFlag)
{Iin = 0; Iout = 0; PrevDig = '*';}
else
{Iin = 1; Iout = 1;
Out[0] = tolower(In[0]); PrevDig = Out[0];}

while ((In[Iin] != (char)NULL) && (Iout <= 4))
{
In[Iin] = tolower(In[Iin]);
switch (In[Iin])
{
case 'b' : C = '1'; break;
case 'p' : C = '1'; break;
case 'f' : C = '1'; break;
case 'v' : C = '1'; break;
case 'c' : C = '2'; break;
case 's' : C = '2'; break;
case 'k' : C = '2'; break;
case 'g' : C = '2'; break;
case 'j' : C = '2'; break;
case 'q' : C = '2'; break;
case 'x' : C = '2'; break;
case 'z' : C = '2'; break;
case 'd' : C = '3'; break;
case 't' : C = '3'; break;
case 'l' : C = '4'; break;
case 'm' : C = '5'; break;
case 'n' : C = '5'; break;
case 'r' : C = '6'; break;
default : C = '*';
}
if ((C != PrevDig) && (C != '*'))
{
Out[Iout] = C;
PrevDig = Out[Iout];
Iout++;
}
Iin++;
}

if (Iout < 4)
for (Iin=Iout; Iin<4; Iin++)
Out[Iin] = '0';

Out[4] = NULL;
return Out;
}

//Main function
int main(int argc, char *argv[])
{
char *Name = (char *)malloc(MAXNAMELEN), *Line = (char *)malloc(MAXLINELEN);
int I;

FullFlag = 0;

if (argc >= 2)
{
if (!strcmp(argv[1], "-f"))
FullFlag = 1;
if ((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "-?")) ||
(!strcmp(argv[1], "?")))
{
printf("");
printf("-f treats the first character of name just like the others. ");
printf("If no names are specified in the arguments, mySoundex will read ");
printf("from the standard input until end of file is encountered. ");
}
}


if (FullFlag && (argc >= 3))
{
for (I=2; I<=(argc-1); I++)
if (strlen(argv[I]) >= 3)
printf("%s ", MySoundex(argv[I]));
printf(" ");
return 0;
}
if ((!FullFlag) && (argc >= 2))
{
for (I=1; I<=(argc-1); I++)
if (strlen(argv[I]) >= 3)
printf("%s ", MySoundex(argv[I]));
printf(" ");
return 0;
}

while (gets(Line) != NULL)
{
while (strcmp(Line, ""))
{
sscanf(Line, "%s", Name);
if (strlen(Name) >= 3)
printf("%s ", MySoundex(Name));
strcpy(Line, delfirstword(Line));
}
printf (" ");
}

free(Line); free(Name);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote