From 6e1d65a63861b39db3a3c5947af6b0cb71db69ea Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 4 Oct 2021 10:17:33 +0200 Subject: [PATCH] fixed a bug that added extra characters in the word obfuscated --- main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 071b907..3532237 100644 --- a/main.c +++ b/main.c @@ -48,28 +48,31 @@ char * recuperationMot(const char file[]) { // pour récupérer un mot aléaotoi return mot; } -int caractereSpecial(char mot) { // vérification que la lettre n'est pas un caractere special - if(mot == ' ') return 1; - if(mot == '\'') return 1; - if(mot == '-') return 1; +int caractereSpecial(char lettre) { // vérification que la lettre n'est pas un caractere special + if(lettre == ' ') return 1; + if(lettre == '\'') return 1; + if(lettre == '-') return 1; return 0; } char * obfuscation(char mot[], int lettresValidees[], int taille) { // pour afficher les trous dans le mot - int tailleMot = taille * 2; - char * motCache = malloc(tailleMot * sizeof(char)); + int tailleMotCache = taille * sizeof(char) * 2; + char * motCache = malloc(tailleMotCache); // x2 pour l'espace entre les "_" int j = 0; for(int i = 0; i < taille; i++) { - if(lettresValidees[i] != 1) { - if(caractereSpecial(mot[i]) == 0) motCache[j] = '_'; else { + if(lettresValidees[i] != 1) { // pas trouvé + if(caractereSpecial(mot[i]) == 0) motCache[j] = '_'; // si c'est pas un caractère spéciale + else { // si c'en est un motCache[j] = mot[i]; lettresValidees[i] = 1; } - } else motCache[j] = mot[i]; + } else motCache[j] = mot[i]; // trouvé motCache[j + 1] = ' '; // on rajoute un espace entre les underscores j = j + 2; } + for(int i = 0; i < longueurMot(motCache); i++) + if(i >= tailleMotCache) motCache[i] = 0; return motCache; }