#include #include #include int hashCode (const char *mot, int n) { int i ; int c = 0 ; for (i=0 ; i< strlen(mot) ; i++ ) { c+=mot[i] ; } return c%n ; } void insererMot (char tab[10][8], const char *mot, int n) { int c ; c = hashCode(mot, n) ; strcpy (tab[c], mot); } void initialiserTab (char tab[10][8], int n) { int i; for (i=0 ; i< n ; i++ ) { tab[i][0]='\0'; } } void afficherTab (char tab[10][8], int n) { int i ; printf("Contenu du tableau\n") ; for (i=0 ; i< n ; i++ ) { printf("%d : %s\n", i, tab[i] ) ; } } int rechercher(char tab[10][8], const char *mot, int n) { int c ; c = hashCode(mot, n) ; if (strcmp(mot, tab[c])==0) { return c ;} else return -1 ; } int main (void) { printf("hashcode de %s : %d\n", "bonjour", hashCode("bonjour", 10)) ; char tab[10][8] ; //tableau de 10 chaines de 8 caractères initialiserTab(tab, 10) ; strcpy (tab[5], "la"); // copie la chaine la à la 5ième ligne du tableau afficherTab(tab, 10) ; insererMot (tab, "salut",10) ; insererMot (tab, "monde",10) ; afficherTab(tab, 10) ; printf("bonjour se trouve à la position %d\n", rechercher(tab, "bonjour", 10)) ; printf("salut se trouve à la position %d\n", rechercher(tab, "salut", 10) ); }