views:

149

answers:

5

Hi,

Assuming these two strings:

string s1="control";
string s2="conrol"; (or "ocntrol", "onrtol", "lcontro" etc.)

How can I programatically find that s2 is similar with s1 and replace the s2 string with the s1 string?

Thanks.

Jeff

+3  A: 

You can use Levenshtein Distance which would give you a rank on how close the two words are. You need to decide at which rank you do the replace .

Shay Erlichmen
+12  A: 

You could try to check the Levenshtein distance between your two words and if the distance is beyond a threshold, replace the word.

The hard part is defining the threshold, in your examples a threshold of 2 could work.

(Implementation of Levenshtein distance in C#)

madgnome
hmmm, it's interesting, and it's fuzzy :)
Jeff Norman
Very interesting. I never knew something like that existed. +1
Alex Essilfie
@madgnome - yep, 2 it's correct
Jeff Norman
+1  A: 

I'll Suggest a simpler answer. Compare the length of the 2 strings & also compare the sum of ASCII values of the both strings.

KhanZeeshan
-1: I like the way you try keep the solution as simple as possible, but this wouldn't even work with the examples that Jeff Norman gave. Levenshtein Distance is definitely the way to go here.
Wouter van Nifterick
KhanZeeshan
A: 

I'd use matlab to run some tests on this. I would do the follow

CONTROL 1111111

OCNTROL 0011111

ONRCTOL 0000111

So I have all 1s for original word, than I have five 1s in a second case and three 1s in a third. You can say that 70% is acceptable and if 70% match than I will use this word. OCNTROL will get accepted, but ONRCTOL won't.

I say Matlab because you can easily load a lot of data into vectors and do vector comparissons.

vikp
A: 

Linq Method: Try to store the chars in both the strings in two List<chars> or List<String> and compare (SequenceEqual or Except) the samller one with the bigger one.

Pramodh