Add strcmp
This commit is contained in:
parent
6abc10a6c4
commit
a1c01f71ac
2 changed files with 60 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -32,3 +32,4 @@ carte-identite
|
|||
conversion-majuscule
|
||||
mirroir
|
||||
creation-mot-de-passe
|
||||
strcmp
|
||||
|
|
59
programmation-c-cpp/tp3/strcmp.c
Normal file
59
programmation-c-cpp/tp3/strcmp.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
/* 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++;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue