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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
// Declaration variable
FILE *pfichier = fopen("MOTS.TXT","w");
char mot[50];
char *tri;
char *temp;
int i,nb_mot=0,a,b;
int longueur=0;
// Initialisation interractive
printf("Combien de mot voulez vous rentrer ? : ");
scanf("%d",&nb_mot);
// Ecriture dans le fichier
for (i = 0; i < nb_mot; ++i)
{
printf("Mot #%d : ",i+1);
scanf("%50s",&mot);
longueur=longueur+strlen(mot);
fprintf(pfichier,"%s\n",mot );
}
tri = malloc(sizeof(char)*longueur);
fclose(pfichier);
// Lecture
pfichier = fopen("MOTS.TXT","r");
if (pfichier==NULL)
{
puts("erreur");
exit(1);
}
i=0;
while (fgets(mot,50,pfichier))
{
printf("%s",mot);
tri=mot;
i++;
}
fclose(pfichier);
for (a = 0; a < nb_mot-1; a++)
{
for (b = a+1; b < nb_mot; b++)
{
if (*(tri+a)>*(tri+b))
{
temp = *(tri+a);
*(tri+a) = *(tri+b);
*(tri+b)= temp;
}
}
}
for (i = 0; i < nb_mot; ++i)
{
printf("%s\n",tri[i] );
}
return 0;
} |
Partager