efrei/programmation-c-cpp/tp3/strcmp.c

59 lines
1.1 KiB
C

/* strcmp */
#include <stdio.h>
#include <stdlib.h>
int strcompare(char*, char*);
void get_alphabetical_order(char*, char*);
int main() {
char mot[100];
char *temoin = "CODEUR";
int i = 0;
printf("Ecrire un mot en majuscule : ");
fgets(mot, 100, stdin);
while (mot[i] != '\n') i++;
mot[i] = '\0';
if (strcompare(mot, temoin)) {
printf("Les mots sont identiques\n");
} else {
get_alphabetical_order(mot, temoin);
}
return 0;
}
int strcompare(char *mot1, char *mot2) {
int i = 0;
while (mot2[i] != '\0') {
if (mot1[i] != mot2[i]) {
return 0;
}
i++;
}
return 1;
}
void get_alphabetical_order(char *mot1, char *mot2) {
int i = 0;
while (1) {
if ((mot1[i] == '\0') || (mot2[i] == '\0')) {
break;
}
if (mot1[i] < mot2[i]) {
printf("%s > %s\n", mot1, mot2);
break;
}
if (mot1[i] > mot2[i]) {
printf("%s > %s\n", mot2, mot1);
break;
}
i++;
}
}