Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
4.00/5 (1 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

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.
 
Share this answer
 
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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900