Levenshtein distance: Difference between revisions

(added ocaml)
Line 53:
<pre>kitten -> sitting: 3
rosettacode -> raisethysword: 8</pre>
 
=={{header|C}}==
Recurive method. Deliberately left in an inefficient state to show the recurive nature of the algorithm; notice how it would have become the wikipedia algorithm if we memoized the function against parameters <code>ls</code> and <code>lt</code>.
<lang C>#include <stdio.h>
#include <string.h>
 
int levenshtein(char *s, int ls, char *t, int lt)
{
int a, b, c;
 
if (!ls) return lt;
if (!lt) return ls;
if (s[ls] == t[lt])
return levenshtein(s, ls - 1, t, lt - 1);
 
a = levenshtein(s, ls - 1, t, lt - 1);
b = levenshtein(s, ls, t, lt - 1);
c = levenshtein(s, ls - 1, t, lt );
 
if (a > b) a = b;
if (a > c) a = c;
 
return a + 1;
}
 
int main()
{
char *s1 = "rosettacode";
char *s2 = "raisethysword";
printf("distance betweeh `%s' and `%s': %d\n", s1, s2,
levenshtein(s1, strlen(s1), s2, strlen(s2)));
 
return 0;
}</lang>
 
=={{header|C sharp|C#}}==
Anonymous user