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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| # include <stdio.h>
# include <stdlib.h>
/* Fonction qui compte le nombre de
caractère de la chaine passé en argument*/
int compte_caract(char * chaine)
{
int i=0;
while(chaine[i]!='\0')
i++;
return i;
}
/* Fonction qui compte le nombre de
mot de la chaine passé en argument*/
int compte_mot(char *chaine)
{
int i,j,mot=0;
i=compte_caract(chaine);
for(j=0;j<=i;j++)
{
if(chaine[j]==' ')
{
mot++;
}
}
return mot+1;
}
/* A REVOIR
Fonction qui decompose une cahine de caractère en un tableau
de caractère.*/
char **decompose_mot(char *chaine)
{
char **tab;
int i,j,a,b,k=0;
i=compte_mot(chaine);
tab=(char **)malloc(i*sizeof(char*));
for(a=0;a<i;a++)
{
b=0;
while(chaine[k]!=' ')
{
tab[a][b]=chaine[k];
b++;
k++;
}
tab[a][b]='\0';
k++;
}
return tab;
}
/* Fonction qui comapare deux chaines et renvoie la difference en
ascii des deux premiers caratères différents sinon renvoie 0.*/
int compare_chaine(char *ch1, char *ch2)
{
int i,t1,t2,k,c;
t1=compte_caract(ch1);
t2=compte_caract(ch2);
if(t1<t2)
c=t1;
else
c=t2;
printf("c= %d\n",c);
for(i=0;i<c;i++)
{
if(ch1[i]!=ch2[i])
return ch1[i]-ch2[i];
}
return 0;
}
// Fonction principale
int main()
{
char *car="AdBmamX toto tata mimi";
char *c="AdBmamX";
printf("%s",decompose_mot(car));
printf("La difference entre ch1 et ch2 est: %d\n",compare_chaine(car,c));
return;
} |
Partager