Created
August 16, 2012 23:44
-
-
Save soarez/3374570 to your computer and use it in GitHub Desktop.
word_diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int partial_match(char *word_a, char *word_b, int len) { | |
int res, idx; | |
res = 0; | |
for(idx = 0; idx < len; ++idx) | |
if (word_a[idx] == word_b[idx]) | |
++res; | |
return res; | |
} | |
int word_diff(char *word_a, char *word_b) { | |
char *small, *big; | |
int len_a, len_b, small_len, big_len, diff; | |
int idx, match, biggest_match; | |
len_a = strlen(word_a); | |
len_b = strlen(word_b); | |
if (len_a > len_b) { | |
big = word_a; | |
small = word_b; | |
big_len = len_a; | |
small_len = len_b; | |
} else { | |
big = word_b; | |
small = word_a; | |
big_len = len_b; | |
small_len = len_a; | |
} | |
biggest_match = 0; | |
diff = big_len - small_len; | |
for(idx = 0; idx <= diff; ++idx) { | |
match = partial_match(small, big + idx, big_len - idx); | |
if (match > biggest_match) | |
biggest_match = match; | |
} | |
return big_len - biggest_match; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment