# C++ Program to Implement Levenshtein Distance Computing Algorithm

«
»
This is a C++ Program to find Levenshtein Distance. The Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
For example, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:

Here is source code of the C++ Program to Implement Levenshtein Distance Computing Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include <stdio.h>`
2. `#include <math.h>`
3. `#include <string.h>`
4. `int d[100][100];`
5. `#define MIN(x,y) ((x) < (y) ? (x) : (y))`
6. `int main()`
7. `{`
8. `    int i,j,m,n,temp,tracker;`
9. `    char s[] = "Sanfoundry";`
10. `    char t[] = "Education";`
11. `    m = strlen(s);`
12. `    n = strlen(t);`
13. ` `
14. `    for(i=0;i<=m;i++)`
15. `    d[0][i] = i;`
16. `    for(j=0;j<=n;j++)`
17. `    d[j][0] = j;`
18. ` `
19. `    for (j=1;j<=m;j++)`
20. `    {`
21. `        for(i=1;i<=n;i++)`
22. `        {`
23. `            if(s[i-1] == t[j-1])`
24. `            {`
25. `                tracker = 0;`
26. `            }`
27. `            else`
28. `            {`
29. `                tracker = 1;`
30. `            }`
31. `            temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));`
32. `            d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));`
33. `        }`
34. `    }`
35. `    printf("the Levinstein distance is %d\n",d[n][m]);`
36. `    return 0;`
37. `}`

Output:

```\$ g++ LevenshteinDist.cpp
\$ a.out

the Levinstein distance is 9

------------------
(program exited with code: 0)

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube