简体   繁体   中英

Simple caesar cipher in C with no user input

I need to write a program that stores a specific message (SMZVQOPIV) in a set of char variables, and then applies a shift value of 8 before outputting the decrypted message one character at a time. The output should look like a string of characters by using a format string that includes multiple %c specifiers.

I can only find examples of code that require input from the user, and although I just modified this to have variables that already hold the required values, I am struggling to make sense of how the code is supposed to iterate through the string - ie ch = ch - 'Z' + 'A' = 1, what does this actually mean in terms of using operators between letters?

This is what I have so far, although the code outputs [Ub^YWXQ^ as the encrypted message - is there any way to correct the characters that should be letters? Should Z become H?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char message[] = "SMZVQOPIV";
    int i;
    int key = 8;

    printf("%s", message);

    char ch= message [i];

    for(i = 0; message[i] !='\0';++i)
    {
        ch = message[i];

        if(ch >= 'A' && ch <= 'Z'){
            ch = ch + key;
            if (ch > 'z'){
                ch = ch - 'Z' + 'A' - 1;
            }
            message[i] = ch;
        }
    }

    printf("Encrypted Message: %s", message);
    return 0;
}

Could anybody briefly talk me through how the code should look and why please? It's been a while since I did any programming and I mostly used to use Python, so am a bit lost in C.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char message[] = "SMZVQOPIV";
    int i;
    int key = 8;

    printf("%s\n", message);
    /*
    *   "printf("%s", message);" -> "printf("%s\n", message);"
    *   you have to print a newline('\n') by yourself,
    *   it's not automatic as "print" in python
    */


    /*
    *   There was a "char ch= message [i];" 
    *   unecessary and it gives error(i is not initialised)
    */

    for (i = 0; message[i] != '\0'; ++i)
    {

        char ch = message[i];
        /*
        *   "ch = message[i];" -> "char ch = message[i];"
        *   when you define an object in C, you have to specify its type
        */

        if (ch >= 'A' && ch <= 'Z') {
            ch = ch + key;
            if (ch > 'Z') {
                /*
                *   Main error here, but just a blunder
                *   "if (ch > 'z') {" -> "if (ch > 'Z') {"
                *   Upper case is important ;), 'Z' is 90 in decimal,
                *   'z' is 122
                */

                ch = ch - 'Z' + 'A' - 1;
            }
            message[i] = ch;
        }
    }

    printf("Encrypted Message: %s", message);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM