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
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NBRMOTS 3
#define LGMOT 20
void tri_chaine_caract (char tableau[NBRMOTS][LGMOT], int tabLength,
int wordLength)
{
int i, k;
char *temp = malloc (wordLength);
if (temp != NULL)
{
for (i = 0; i < tabLength; i++)
{
k = i;
while ((k >= 0) || (strcmp (tableau[k], tableau[k - 1]) < 0))
{
strcpy (temp, tableau[k]);
strcpy (tableau[k], tableau[k - 1]);
strcpy (tableau[k - 1], temp);
k--;
}
}
free (temp);
}
}
static void purge (void)
{
int c;
while ((c = getchar ()) != '\n' && c != EOF)
{
}
}
int main (void)
{
char tab[NBRMOTS][LGMOT];
int i;
int choix;
printf ("Saisir une série de 3 mots! \n");
for (i = 0; i < NBRMOTS; i++)
{
printf ("Mot %d : ", i);
scanf ("%s", tab[i]);
purge ();
}
for (i = 0; i < NBRMOTS; i++)
{
printf ("Mot %d : %s\n", i, tab[i]);
}
do
{
printf ("'r' pour effectuer le tri \n");
printf ("'q' pour quitter \n\n");
choix = getchar ();
if (choix != '\n')
{
purge ();
}
if (choix == 'r')
{
tri_chaine_caract (tab, NBRMOTS, LGMOT);
for (i = 0; i < NBRMOTS; i++)
{
printf ("Mot %d : %s\n", i, tab[i]);
}
}
}
while (choix != 'q');
return 0;
} |
Partager