Solved

explaining compname () algorithms

Posted on 2001-08-02
6
187 Views
Last Modified: 2010-04-15
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
Comment
Question by:korsila
  • 4
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 30 total points
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

by:korsila
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

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

thanxxxxxxxxxxxxxxx....
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Expert Comment

by:imladris
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

by:korsila
ID: 6382211
0
 

Author Comment

by:korsila
ID: 6382217
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now