Click here to Skip to main content
14,244,088 members
Rate this:
Please Sign up or sign in to vote.
i want to compare two strings and calculate the percentage of difference between them. This could have been easier for two strings of same length. But what if the length is different? For eg :

hello my name is amit and i like to help everybody

hi my name is amit and i would like to help every coder

I want to calculate the percentage of difference between them

A simple logic would be to compare each word in first sentence with same position word of different sentence. But in this case the length is different. So how do i proceed?

Any help would be appreciated.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 2

Basically you need to decide on a string metric and then calculate the difference for your strings. There are many possible metrics, so it depends on how you want to define degrees of "different."

See here for start[^]. As mentioned, the Levenshtein metric is probably the most common.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

See here[^].

If you can get an array back (containing all the differences as is provided by the author here), you can calculate the percentage by calculating the differences versus the original string lengths.
   
v2
Comments
amit_upadhyay 28-Nov-10 1:01am
   
I will go through the code. But a little doubt : "An array of items containing the differences is returned." - This returns words from both the strings or only one string (former or later)? Or both is important to calculate the difference?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100