I am trying to implement edit string algorithm given in:
http://en.wikipedia.org/wiki/Levenshtein_distance
in C and I am repeatedly getting "segmentation Fault" I dont understand why......:(
Here is my code
#include<stdio.h>
#include<string.h>
int minimum(int a,int b,int c)
{
int min;
if(a<b)
{
if(a<c) min=a;
else min=c;
}
else
{
if(c<b) min=c;
else min=b;
}
return min;
}
int edit_string(char *p,char *q)
{
int len_p=strlen(p),len_q=strlen(q);
int cost_matrix[100][100];
int i,j;
int cost;
for(i=0;i<100;i++)
for(j=0;j<100;j++) cost_matrix[i][j]=0;
for(j=0;j<=len_q;j++)
cost_matrix[0][j]=j;
for(i=0;i<=len_p;i++)
cost_matrix[i][0]=i;
for(i=1;i<=len_p;i++)
{
for(j=1;j<=len_q;j++)
{
if(p[i-1]==q[j-1]) cost=0;
else cost=1;
cost_matrix[i][j]=minimum(cost_matrix[i-1][j],cost_matrix[i][j-1],cost_matrix[i-1][j-1]+cost);
}
}
return cost_matrix[len_p][len_q];
}
int main()
{
char *str1,*str2;
int ans;
gets(str1);
gets(str2);
ans=edit_string(str1,str2);
printf("\n%d",ans);
return 0;
}
I think the code is self explanatory....
Thanx for any suggestion....