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

79 lines
1.5 KiB
C
Raw Normal View History

2021-11-14 23:32:13 +01:00
/* 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) {
2021-11-15 08:24:22 +01:00
int i = 0, len1 = 0, len2 = 0;
while (mot1[len1] != '\0') {
len1++;
}
while (mot2[len2] != '\0') {
len2++;
}
if (len1 != len2) {
return 0;
}
2021-11-14 23:32:13 +01:00
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) {
2021-11-15 08:24:22 +01:00
if ((mot1[i] == '\0') && (mot2[i] != '\0')) {
printf("%s > %s\n", mot1, mot2);
break;
}
if ((mot1[i] != '\0') && (mot2[i] == '\0')) {
printf("%s > %s\n", mot2, mot1);
2021-11-14 23:32:13 +01:00
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;
}
2021-11-15 08:24:22 +01:00
if ((mot1[i] == '\0') || (mot2[i] == '\0')) {
break;
}
2021-11-14 23:32:13 +01:00
i++;
}
}