int main(int argc, char** argv){ if(argc < 3){ printf("Usage: %s word1 word 2\n", argv[0]); exit(1); } printf("The distance between %s and %s is %d.\n", argv[1], argv[2], editDist(argv[1], argv[2])); return 0; }
/* Driver program */ int main() { char str1[] = "sunday"; char str2[] = "saturday"; int str1Length = sizeof(str1) / sizeof(str1[0]); int str2Length = sizeof(str2) / sizeof(str2[0]); memset(memo, -1 ,sizeof memo); int minimum = editDist(str1, str2, str1Length, str2Length); printf("%d\n", minimum); return 0; }
int editDist(char str1[], char str2[], int m, int n) { /* * If first string is empty, the only option is to * insert all characters of second string into first */ if (m == 0) return n; /* * If second string is empty, the only option is to * remove all characters of first string */ if (n == 0) return m; /* If this state has already been computed, get its value */ if (memo[m][n] > -1) return memo[m][n]; /* * If last characters of two strings are same, nothing * much to do. Ignore last characters and get count for * remaining strings. */ if (str1[m-1] == str2[n-1]) return memo[m][n] = editDist(str1, str2, m-1, n-1); /* * If last characters are not same, consider all three * operations on last character of first string, recursively * compute minimum cost for all three operations and take * minimum of three values. */ return memo[m][n] = 1 + min ( editDist(str1, str2, m, n-1), /* Insert */ editDist(str1, str2, m-1, n), /* Remove */ editDist(str1, str2, m-1, n-1) /* Replace */ ); }