C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Code that compares two strings and returns % of likeness Posted by Octavio on 15 Sept 2005 at 9:49 PM
I need to program a routine that will receive two strings and return % (from 0% to 100%) of likeness between the two strings.
For example:

I compare these two strings:
Hello Jhon
Hello Jon
And the routine tells me that theres a 90% of likeness between the two of them.

I dont have any idea where to start, but i gues this is what goggle does when you search for something and it tells you if you meant something else.

Does anyone know where can i start or send me a link with some tutorial? I have spent days searching with no use.

Thanks in advance!


Report
Re: Code that compares two strings and returns % of likeness Posted by Lundin on 15 Sept 2005 at 11:28 PM
: I need to program a routine that will receive two strings and return % (from 0% to 100%) of likeness between the two strings.
: For example:
:
: I compare these two strings:
: Hello Jhon
: Hello Jon
: And the routine tells me that theres a 90% of likeness between the two of them.
:
: I dont have any idea where to start, but i gues this is what goggle does when you search for something and it tells you if you meant something else.
:
: Does anyone know where can i start or send me a link with some tutorial? I have spent days searching with no use.
:
: Thanks in advance!
:
:
:


Start to compare length of the strings with strlen. If they aren't equal, loop through the shortest of them and check every character with the corresponding character in the other string.

You have to decide what to do if they aren't equal: will you skip that char, decrease the likeness % and go to the next. Or will you do something else?

You will have to think of things like this:

Hello Jhon
Hello Jon

or

Hello Jhon
Hello J on

Are the two examples as much alike? Maybe you will have to check through the strings several times with different algorithms. Are they not equal if they are of different upper/lower case? And so on...
Report
Re: Code that compares two strings and returns % of likeness Posted by Octavio on 16 Sept 2005 at 7:33 AM
: : I need to program a routine that will receive two strings and return % (from 0% to 100%) of likeness between the two strings.
: : For example:
: :
: : I compare these two strings:
: : Hello Jhon
: : Hello Jon
: : And the routine tells me that theres a 90% of likeness between the two of them.
: :
: : I dont have any idea where to start, but i gues this is what goggle does when you search for something and it tells you if you meant something else.
: :
: : Does anyone know where can i start or send me a link with some tutorial? I have spent days searching with no use.
: :
: : Thanks in advance!
: :
: :
: :
:
:
: Start to compare length of the strings with strlen. If they aren't equal, loop through the shortest of them and check every character with the corresponding character in the other string.
:
: You have to decide what to do if they aren't equal: will you skip that char, decrease the likeness % and go to the next. Or will you do something else?
:
: You will have to think of things like this:
:
: Hello Jhon
: Hello Jon
:
: or
:
: Hello Jhon
: Hello J on
:
: Are the two examples as much alike? Maybe you will have to check through the strings several times with different algorithms. Are they not equal if they are of different upper/lower case? And so on...
:


Thank you, im gonna try
Report
Re: Code that compares two strings and returns % of likeness Posted by istrasci on 16 Sept 2005 at 7:57 AM
: I need to program a routine that will receive two strings and return % (from 0% to 100%) of likeness between the two strings.
: For example:
:
: I compare these two strings:
: Hello Jhon
: Hello Jon
: And the routine tells me that theres a 90% of likeness between the two of them.
:
: I dont have any idea where to start, but i gues this is what goggle does when you search for something and it tells you if you meant something else.
:
: Does anyone know where can i start or send me a link with some tutorial? I have spent days searching with no use.
:
: Thanks in advance!
:

My question is what does the percentage really mean...??? In your example of comparing

Hello Jhon
Hello Jon

They match 9 letters... They match 9 out of 10 letters in the first string (90%), but match 9 out of 9 in the second one (100%)... If you average them, that's 95%... So you really need to have a solid meaning of the percentage, because there could be many possibilities... Also, how do you define the percentage for this example???:

Hello Jhon
Hello John

They match 100% in the letters they contain, but one (and esp. strcmp() ) would definitely not say that they are matching strings... Maybe 70% since the first 7 letters match, but then this goes back to the first problem if the strings are different lengths...

Tough problem... Good luck... Like I said, once you have a good meaning defined for the percentage, it will be easier to figure out what to do...
Report
Re: Code that compares two strings and returns % of likeness Posted by Octavio on 16 Sept 2005 at 10:20 AM
: : I need to program a routine that will receive two strings and return % (from 0% to 100%) of likeness between the two strings.
: : For example:
: :
: : I compare these two strings:
: : Hello Jhon
: : Hello Jon
: : And the routine tells me that theres a 90% of likeness between the two of them.
: :
: : I dont have any idea where to start, but i gues this is what goggle does when you search for something and it tells you if you meant something else.
: :
: : Does anyone know where can i start or send me a link with some tutorial? I have spent days searching with no use.
: :
: : Thanks in advance!
: :
:
: My question is what does the percentage really mean...??? In your example of comparing
:
: Hello Jhon
: Hello Jon
:
: They match 9 letters... They match 9 out of 10 letters in the first string (90%), but match 9 out of 9 in the second one (100%)... If you average them, that's 95%... So you really need to have a solid meaning of the percentage, because there could be many possibilities... Also, how do you define the percentage for this example???:
:
: Hello Jhon
: Hello John
:
: They match 100% in the letters they contain, but one (and esp. strcmp() ) would definitely not say that they are matching strings... Maybe 70% since the first 7 letters match, but then this goes back to the first problem if the strings are different lengths...
:
: Tough problem... Good luck... Like I said, once you have a good meaning defined for the percentage, it will be easier to figure out what to do...
:

Good point. The code should be able to tell differences likes extra spaces, added symbols or any lack of symbol. This is getting though, ive been investigating and its harder than i thought; there are many algorithms that have been created before to solve this, but they are not very simple to understand though.
Report
Re: Code that compares two strings and returns % of likeness Posted by Chainsaw666 on 16 Sept 2005 at 9:09 PM
Like everyone has been saying, it really depends on what kind of comparison you wish to do. If you want to just compare the occurrance of letters, you could just use an array of chars, if you want to compare the arrangements you'd need to loop through each to search for similar letters. What exactly do you want to get as far as similarities are concerned.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.