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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* -tc- se renseigner sur fclean() en effectuant une recherche sur le forum C */
static void fclean(char *tampon, FILE *fp);
int main (void)
{
char str1[10] = "";
char str2[10] = "";
int n;
printf ("Entrez le 1er mot: ");
/* -tc- fflush(stdout) est necessire pour forcer l'affichage de l'invite */
fflush(stdout);
fgets(str1, sizeof str1, stdin);
fclean(str1, stdin);
printf("Entrez le 2eme mot: ");
fflush(stdout);
fgets(str2, sizeof str2, stdin);
fclean(str2, stdin);
n = strcmp (str1, str2);
if (n == 0)
{
printf ("Les chaines %s et %s sont identiques\n", str1, str2);
}
else
{
printf ("La chaine %s est inferieure a %s\n", (n < 0) ? str1 : str2,
(n < 0) ? str2 : str1);
}
return EXIT_SUCCESS;
}
/* -tc- se renseigner sur fclean() en effectuant une recherche sur le forum C */
static void
fclean(char *tampon, FILE *fp)
{
char *pc = strchr(tampon, '\n');
if (pc != NULL)
{
*pc = 0;
}
else
{
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
}
}
} |
Partager