Solved

# explaining compname () algorithms

Posted on 2001-08-02
Medium Priority
203 Views
Dear Expert, especially to imladris who has implemented these following codes...

I just had a chance to have a closer look at all of your codes -Imladris...and i found out that 3 of your codes (3 files below) which u have implemented, are quiet different especially the output I got....
could you comment and explain each code  and please illustrate how they are different from each other....am a bit confused and it was even more when I got 3 differnt outputs from them..
p.s. please explain clearly especially how different of each codes....

/*file1 */
int compname(char *nm1,char *nm2)
{   int i,l1,l2,len;
float same,diff;

l1=strlen(nm1);
l2=strlen(nm2);
len=(l1>l2?l1:l2);
same=0.;
diff=0.;
for(i=0; i<len; ++i)
{   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
else diff+=1.;
}
return(same/(same+diff)>0.5);
}

int compchar(char a,char b)
{     return(toupper(a)==toupper(b));
}

--------------------
/*file2 */
float compname(char *nm1,char *nm2)
{   int i,j,k,l1,l2,len;
float same,diff;

same=diff=0.;
l1=strlen(nm1);
l2=strlen(nm2);
j=0;
for(i=0; i<l1; ++i)
{   if(compchar(nm1[i],nm2[j]))
{   same+=1.;
++j;
}
else
{   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
if(k<l2)
{   same+=1.;
diff+=(float)(k-j);
j=k+1;
}
}
}
if(same==0. && diff==0.)return(0.);
return(same/(same+diff));
}

--------------------
/* file3 */
int compname(char *nm1,char *nm2)
{   int i,l1,l2,len;
float same,diff;

l1=strlen(nm1);
l2=strlen(nm2);
len=(l1>l2?l1:l2);
same=0.;
diff=0.;
for(i=0; i<len; ++i)
{   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
else diff+=1.;
}
return(same/(same+diff)>0.5);
}
-----------------------

0
Question by:korsila
[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
• 4
• 2

LVL 16

Accepted Solution

ID: 6345942
Yes, these compnames are from when we started with different algorithms for determining whether names matched or not. I don't remember what the programs were called, but here is what they appear to do:

/*file1 */

// return true, indicating that nm1 matches nm2 if
// the number of identical letters, divided by total
// letters is greater than 1/2.
// This algorithm simply compares characters by position
// i.e. smith and smath have 4 same and 1 diff
// and  smith and smeeth have 2 same and 3 diff
int compname(char *nm1,char *nm2)
{   int i,l1,l2,len;
float same,diff;

// get length of names
l1=strlen(nm1);
l2=strlen(nm2);

//use greatest length for loop
//initialize same and different
//letter counters
len=(l1>l2?l1:l2);
same=0.;
diff=0.;

//for each letter in the longest name
//  if both names have letters and letters
//      are the same, increment same
//  else increment diff
for(i=0; i<len; ++i)
{   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
else diff+=1.;
}

// return match if same/(same+diff) > 0.5
return(same/(same+diff)>0.5);
}

//compare two characters, ignoring case
//  done by making both uppercase
int compchar(char a,char b)
{     return(toupper(a)==toupper(b));
}

/*file2 */
//return match value for nm1 and nm2 this compname
//This algorithm tracks the last matching letters
//i.e. smith and smath have 4 same and 1 diff
//and smith and smeeth also have 4 same and 2 diff
float compname(char *nm1,char *nm2)
{   int i,j,k,l1,l2,len;
float same,diff;

//initialize same and diff letter counters
//and nm lengths
//initialize j: last matched position
same=diff=0.;
l1=strlen(nm1);
l2=strlen(nm2);
j=0;

//for all letters in nm1
for(i=0; i<l1; ++i)
{   //check letters in nm1 versus nm2
//if they're the same
//   increment same and j
if(compchar(nm1[i],nm2[j]))
{   same+=1.;
++j;
}

//else
//  compare current nm1 character
//  against all characters in nm2
//  from last matched position to end
//  if match found
//    increment same
//    increase diff by number of
//       letters skipped
//       set j to new last matched
else
{   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
if(k<l2)
{   same+=1.;
diff+=(float)(k-j);
j=k+1;
}
}
}

//if null case and both same and diff
//are 0, return match value of 0
//else return same / (same + diff)
if(same==0. && diff==0.)return(0.);
return(same/(same+diff));
}

/* file3 */
//This one looks the same as file1 to me
int compname(char *nm1,char *nm2)
{   int i,l1,l2,len;
float same,diff;

l1=strlen(nm1);
l2=strlen(nm2);
len=(l1>l2?l1:l2);
same=0.;
diff=0.;
for(i=0; i<len; ++i)
{   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
else diff+=1.;
}
return(same/(same+diff)>0.5);
}

0

Author Comment

ID: 6351490
oops sory "file3" is look like this ..a bit different from "file2"

float compname(char *nm1,char *nm2)
{   int i,j,k,l1,l2,len;
float same,diff;

same=diff=0.;
l1=strlen(nm1);
l2=strlen(nm2);
j=0;
for(i=0; i<l1; ++i)
{   if(compchar(nm1[i],nm2[j]))
{   same+=1.;
++j;
}
else
{   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
if(k<l2)
{   same+=1.;
diff+=(float)(k-j);
j=k+1;
}
}
}
if(j<l2)diff+=(float)(l2-j);----here the different from file 2
return(same/(same+diff));
}

0

Author Comment

ID: 6351500
explain here a bit more when u get back from holiday...
if(j<l2)diff+=(float)(l2-j);

thanxxxxxxxxxxxxxxx....
0

LVL 16

Expert Comment

ID: 6380232
Ah, j is the last matched letter. That line takes care of the case when a trailing set of letters in name2 produces no matches. In that case all the letters from j until the end of the string are different and should be added to diff.
0

Author Comment

ID: 6382211
0

Author Comment

ID: 6382217
0

## Featured Post

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month10 days, 15 hours left to enroll