Friday, 24 November 2017

C++ Program to Implement Levenshtein Distance Computing Algorithm


Code:

#include    stdio.h
#include    math.h
#include    string.h
int d[100][100];
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int main()
{
    int i,j,m,n,temp,tracker;
    char s[] = "Sanfoundry";
    char t[] = "Education";
    m = strlen(s);
    n = strlen(t);

    for(i=0;i<=m;i++)
    d[0][i] = i;
    for(j=0;j<=n;j++)
    d[j][0] = j;

    for (j=1;j<=m;j++)
    {
        for(i=1;i<=n;i++)
        {
            if(s[i-1] == t[j-1])
            {
                tracker = 0;
            }
            else
            {
                tracker = 1;
            }
            temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
            d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
        }
    }
    printf("the Levinstein distance is %d\n",d[n][m]);
    return 0;
}



Output:

the Levinstein distance is 9

------------------
(program exited with code: 0)
Press return to continue



More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...