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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <stdio.h>
#include <stdlib.h>
typedef struct adr
{
char *ptrdebut;
char *ptrfin;
} ptr;
int main()
{
char tab[]="Bonjour tout le monde monsieur";
int i,j,k,l,nbr,flag ;
ptr *ptrind;
nbr = 0;
printf("\nPhrase : %s\n",tab);
l = sizeof(tab) - 1;
flag = 0;
for(i = 0; i <= l; i++)
{
if ((tab[i] != ' ') && (flag == 0))
{
nbr = nbr + 1;
flag = 1;
}
else
{
if((tab[i] == ' '))
{
flag = 0;
}
}
}
printf("\nLongueur de la phrase : %d", l);
printf("\nNombre de mot : %2d\n", nbr);
j = 0;
ptrind = malloc(nbr * sizeof(ptr));
if (ptrind != NULL)
{
printf("\nallocation fonctionnel\n"); // affiche si l'allocation à fonctionner
flag = 0;
for (i = 0; i < l ; i++)
{
if ((tab[i] != ' ') && (flag == 0))
{
//printf("%c, %d\n",tab[i], &tab[i]); // permettait l'affichage de la première lettre du mot
printf("%d - %c \n", &tab[i], tab[i]);
ptrind[j].ptrdebut = &tab[i];
flag = 1;
}
else
{
if ((tab[i] == ' ') && (flag == 1))
{
//printf("%c, %d\n",tab[i - 1], &tab[i - 1]); // permettait l'affichage de la dernière lettre du mot
printf("%d - %c \n", &tab[i - 1], tab[i - 1]);
ptrind[j].ptrfin = &tab[i - 1];
j = j + 1;
flag = 0;
}
}
}
ptrind[j].ptrfin = &tab[i-1]; //allocation de la dernière adresse de la lettre du dernier mot
printf("%d - %c \n", &tab[i - 1], tab[i - 1]);
printf("\nAfficher la phrase complete : %s", tab);
printf("\nAfficher mot a mot\n\n");
/*
for(i = 0; i <= nbr - 1; i++)
{
printf("Mot N°%d %d , %d\n",i + 1, ptrind[i].ptrdebut, ptrind[i].ptrfin );
}
*/
j = 0; //je calcule le premier indice de début du premier mot
l = (int)(ptrind[j].ptrfin - ptrind[j].ptrdebut); //je calcule le premier indice de fin du premier mot
for(i = 0; i <= nbr - 1; i++) // boucle parcourant le record
{
printf("%2d - %2d ", j, l);
printf("Mot N%d : ", i + 1); // affichage du numéro de mot
for(k = j; k <= l ; k++) // boucle permettant de donner chacune des lettres d'un mot
{
printf("%c",tab[k]); //j'affiche la lettre à lettre sur une même ligne tant qu'on a pas fini le mot
}
printf("\n");
j = j + (int)(ptrind[i].ptrfin - ptrind[i].ptrdebut) + (int)(ptrind[i + 1].ptrdebut - ptrind[i].ptrfin);
//calcul me donnant donc l'indice de début par le biais des adresses des prochains mots
l = j + (int)(ptrind[i + 1].ptrfin - ptrind[i + 1].ptrdebut);
// me donne donc l'indice de fin par le biais des adresses
}
printf("\n%d\n",&tab); // adresse du tableau
// Le problème se situe ici
for(k= 0; k <= nbr - 1; k++)
{
printf("%2d - %2d ", ptrind[k].ptrdebut, ptrind[k].ptrfin);
printf("Mot N%d : ", k + 1); // affichage du numéro de mot
for (i = (int)ptrind[k].ptrdebut; i <= (int)ptrind[k].ptrfin; i++)
{
printf("%c ", i);
}
printf("\n");
}
free(ptrind);
}
return 0;
} |
Partager