Code César : déchiffrage des minuscules
Bonjour,
Le programme marche très bien sauf dans le cas déchiffrage des minuscules.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void cesar_crypt (int decallage, char *texte)
{
/* Explication de la conversion pour un caractere minuscule
texte[i] - 'a' -> on recupere un nombre represantante le caractere contenue
dans texte[i] (0=a, 1=b, ...)
(texte[i] - 'a') + decallage -> on lui applique le decallage
((texte[i] - 'a') + decallage)%26 -> on fait %26 pour revenir au debut si on a depasser 'z'
'a' + ((texte[i] - 'a') + decallage)%26 -> on retrouve le bon caractere en ajoutant 'a'
*/
int i;
for(i=0 ; i<strlen(texte) ; i++)
/* Si le caractere est une minuscule */
if ('a' <= texte[i] && texte[i] <= 'z')
texte[i] = 'a' + ((texte[i] - 'a') + decallage)%26;
else
/* Si le caractere est une majuscule */
if ('A' <= texte[i] && texte[i] <= 'Z')
texte[i] = 'A' + ((texte[i] - 'A') + decallage)%26;
}
void cesar_decrypt (int decallage, char *texte)
{
/* Explication de la conversion pour un caractere minuscule
texte[i] - 'a' -> on recupere un nombre represantante le caractere contenue
dans texte[i] (0=a, 1=b, ...)
(texte[i] - 'a') - decallage -> on lui applique le decallage
((texte[i] - 'a') - decallage)%26 -> on fait %26 pour revenir au debut si on a depasser 'z'
'a' + ((texte[i] - 'a') - decallage)%26 -> on retrouve le bon caractere en ajoutant 'a'
*/
int i;
for(i=0 ; i<strlen(texte) ; i++)
/* Si le caractere est une minuscule */
if ('a' <= texte[i] && texte[i] <= 'z')
texte [i] ='a' + (((texte[i]-'a')-decallage)%26);
else
/* Si le caractere est une majuscule */
if ('A' <= texte[i] && texte[i] <= 'Z')
texte[i] = 'A' + ((texte[i] + 'A') - decallage)%26;
}
int main()
{
char Test[1000];
int n;
puts("donner votre texte");
gets(Test);
puts("donner votre code");
scanf("%d",&n);
cesar_crypt(n, Test);
printf(" votre cryptage est:%s\n", Test);
cesar_decrypt(n,Test);
printf("votre decryptage est:%s\n", Test);
return EXIT_SUCCESS;
} |
Pouvez-vous m'aider ?
Merci.