Comparer deux chaine de caractères (vérification de mon algo svp)
Bonjour tout le monde,
J'aimerais créer un algorithme qui renvoi qui compare deux chaines de caractères.
Il y'a donc chaine1 et chaine2.
Chaques chaine se terminent par EOS (End of String)
J'aimerais retourner 1 si chaine1 est plus grande que chaine2 :
Exemple :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
Chaine1 | B | O | N | J | O | U | R |EOS|
+---+---+---+---+---+---+---+---+
(!)
0 1 2 3
+---+---+---+---+
Chaine2 | A | M | I |EOS|
+---+---+---+---+
(!)
B > A
===> 1 |
Retourne - 1 si la première chaine est plus petite que la 2e chaine :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
Chaine1 | B | O | N | J | O | U | R |EOS|
+---+---+---+---+---+---+---+---+
(!)
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
Chaine2 | B | O | N | S | O | I | R |EOS|
+---+---+---+---+---+---+---+---+
(!)
N < S
===> -1 |
Retourne 0 au cas où la chaine est la même :
Code:
1 2 3 4 5 6 7 8 9 10 11
| 0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
Chaine1 | B | O | N | J | O | U | R |EOS|
+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+---+
Chaine2 | B | O | N | J | O | U | R |EOS|
+---+---+---+---+---+---+---+---+
===> 0 |
Le problème est que si la chaine est plus longue que l'autre, je ne sais pas si EOS est considéré comme un caractère.
Voici ce que j'ai fait, sauriez-vous me dire ce que vous en pensez ?
Je doute surtout au niveau des ligne qui sont en rouge :
Code:
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
| FONCTION ComparaisonDeuxChaines(Chaine1,Chaine2)
Parametres Chaine1, Chaine2 : CHAINE DE CARACTERE[I];
RETOUR : ENTIER;
VARIABLE : Indice;
DEBUT
Indice = 0;
TANT QUE ((Chaine1[Indice] || Chaine2[Indice])!=EOS) FAIRE
SI Chaine1[Indice] != Chaine2[Indice] ALORS
SI Chaine1[Indice] < Chaine2[Indice] || Chaine1[Indice] == EOS ALORS
RETOURNER -1;
AUTRE SI Chaine[Indice] > Chaine2[Indice] || Chaine2[Indice] == EOS ALORS
RETOURNER 1;
FIN SI
FIN SI
FIN SI
Indice++;
FIN TANT QUE
RETOURNER 0;
FIN |
Merci d'avance pour votre aide.
beegees