Challenge Distance de Levenshtein
Bonjour, je débute en C. J'essaye de faire des exercices par moi-même afin de progresser.
J'essaye ici de programmer l'algorithme de la distance de Levenshtein https://fr.wikipedia.org/wiki/Distance_de_Levenshtein
Quand je lance mon programme j'ai un Segmentation fault(core dumped). Pourtant j'ai bien suivi l'algorithme sur wikipédia.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int levenshtein(char* s1, char* s2){
int longueurChaine1=strlen(s1);
int longueurChaine2=strlen(s2);
int d[longueurChaine1+1][longueurChaine2+1];
int i, j, coutSubstitution;
int tmp=0;
for(i=0;i<longueurChaine1;i++)
{
d[i][0] = i;
}
for(j=0;j<longueurChaine2;j++)
{
d[0][j] = j;
}
for(i=1;i<=longueurChaine1;i++)
{
for(j=1;j<=longueurChaine2;j++)
{
if(s1[i-1] = s2[j-1])
{
coutSubstitution = 0;
}
else
{
coutSubstitution = 1;
}
printf(" deb %d ", d[i][j]);
if ( (d[i-1][j] + 1) < (d[i][j-1] + 1))
{
d[i][j] = d[i-1][j] + 1;
}
else{
d[i][j] = d[i][j-1] + 1;
}
printf(" inter %d ", d[i][j]);
if(d[i][j] > (d[i-1][j-1] + coutSubstitution))
{
d[i][j] = d[i-1][j-1] + coutSubstitution;
}
printf("avn last %d ", d[i][j]);
tmp = d[i][j];
}
}
//printf(" last %d ", tmp);
return tmp;
}
int main(){
printf("%d",levenshtein("niche", "chien"));
return 0;
} |
Je vous remercie d'avance pour tout aide
Challenge Distance de Levenshtein
Merci ça marche beaucoup mieux.