54 lines
1.8 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void decode(char *input) {
bool found = false;
int decalage = 1;
char base ='A';
while (found == false) {
// We only need to try 25 shifts
// Copy the input to avoid modifying the original
// Apply the current shift to each character
printf("Décalage: %d \n", decalage);
for (int i = 0; input[i] != '\0'; i++)
if (input[i] >= 'A' && input[i] <= 'Z')
printf("%c",((input[i] - base + decalage) % 26) + base);
else
printf("%c",input[i]);
// Display the shifted text
// Ask the user if this is the correct decoding
printf("\n \nC'est correct? (y/n): ");
char response;
scanf(" %c", &response); // Note the space before %c to consume whitespace
printf("\n");
if (response == 'y' || response == 'Y') {
found = true;
printf("\nDecodage réussi avec le décalage %d.\n", decalage);
} else {
decalage++;
}
}
}
int main() {
char text[] = "DQDM TM KWLQVO IVL OIUQVO\n"
"TM RWCZ LM AMA WVHM IVA, PIZZG XWBBMZ, CV WZXPMTQV MTMDM XIZ CV WVKTM MB CVM\n"
"BIVBM YCQ TM LMBMABMVB, DWQB AWV MFQABMVKM JWCTMDMZAMM. CV OMIVB DQMVB TM\n"
"KPMZKPMZ XWCZ T'MUUMVMZ I XWCLTIZL, CVM MKWTM LM AWZKMTTMZQM ! DWTMZ MV JITIQ,\n"
"RMBMZ LMA AWZBA, KWUJIBBZM TMA BZWTTA : PIZZG ZMDMTM LM OZIVLA BITMVBA. UIQA\n"
"CV UGABMZM MVBWCZM AI VIQAAIVKM MB T'MNNZWGIJTM D. TM UIOM LWVB XMZAWVVM\n"
"V'WAM XZWVWVKMZ TM VWU.";
printf("Texte Original:\n%s\n\n", text);
printf("Décodage...\n");
decode(text);
return 0;
}