简体   繁体   English

C 中的简单凯撒密码,无需用户输入

[英]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.我需要编写一个程序,将特定消息 (SMZVQOPIV) 存储在一组 char 变量中,然后在一次输出一个字符的解密消息之前应用移位值 8。 The output should look like a string of characters by using a format string that includes multiple %c specifiers.通过使用包含多个 %c 说明符的格式字符串,output 应该看起来像一个字符串。

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?我只能找到需要用户输入的代码示例,虽然我只是将其修改为具有已经包含所需值的变量,但我正在努力理解代码应该如何遍历字符串 - 即 ch = ch - 'Z' + 'A' = 1,就在字母之间使用运算符而言,这实际上意味着什么?

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?这就是我到目前为止所拥有的,尽管代码输出 [Ub^YWXQ^ 作为加密消息 - 有没有办法纠正应该是字母的字符? Should Z become H? Z应该变成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.自从我做任何编程以来已经有一段时间了,我主要习惯使用 Python,所以在 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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