Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 凯撒密码中最后一个字母后循环回字母表开头的逻辑_C_Loops_Ascii_Cs50_Caesar Cipher - Fatal编程技术网

C 凯撒密码中最后一个字母后循环回字母表开头的逻辑

C 凯撒密码中最后一个字母后循环回字母表开头的逻辑,c,loops,ascii,cs50,caesar-cipher,C,Loops,Ascii,Cs50,Caesar Cipher,我一直在编写下面的程序作为Caesar密码的一个实现,除非提供的密钥大于26,否则它是有效的。问题是,一旦值超过'z'或'z',我不知道如何循环 到目前为止,我尝试的是嵌套if语句和while循环,这导致了内存泄漏LOL,所以我可以说,我被卡住了 如何使用该程序: gcc-o caesar-caesar.c-lcs50-lm&./caesar键号,其中caesar.c是文件名 #include <cs50.h> #include <stdio.h> #include &l

我一直在编写下面的程序作为Caesar密码的一个实现,除非提供的密钥大于26,否则它是有效的。问题是,一旦值超过'z'或'z',我不知道如何循环

到目前为止,我尝试的是嵌套if语句和while循环,这导致了内存泄漏LOL,所以我可以说,我被卡住了

如何使用该程序: gcc-o caesar-caesar.c-lcs50-lm&./caesar键号,其中caesar.c是文件名

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

bool input_valid(int count, string arg);

int main(int argc, string argv[])
{
    int key;
    char final_val;
    string string;
    char cipher_string[80];

    if (!input_valid(argc, argv[1]))
    {
        printf("Invalid input!\nUSAGE: ./caesar key\n");
        return 1;
    }

    string = get_string("plaintext: ");
    key = strtol(argv[1], NULL, 10);

    for (int i = 0; i < strlen(string); i++)
    {
        int ascii_val = (int)string[i];

        bool valid_lower_case = (ascii_val + key) >= 'a' && (ascii_val + key) < 'z';
        bool valid_upper_case = (ascii_val + key) >= 'A' && (ascii_val + key) < 'Z';

        if (isalpha(string[i]))
        {
            if (valid_lower_case || valid_upper_case)
            {
                final_val = ascii_val + key;
            }
            else
            {
                // loop back to 'a' if ascii_val reaches 'z'
                final_val = 'a' + (key - ('z' - (ascii_val - 1)));
            }
        }
        else
        {
            final_val = ascii_val;
        }

        cipher_string[i] = final_val;
    }

    printf("ciphertext: %s\n", cipher_string);
}

bool input_valid(int count, string arg)
{
    // input has more args than just the file name
    // input is an integer
    return count > 1 && isdigit(arg[0]);
}

你熟悉MOD运算符吗,%

它对于实现面向环的数据非常有用,例如

array = [0, 1, 2] // "ring"

array[0 % 3] == 0
array[1 % 3] == 1
array[2 % 3] == 2
array[3 % 3] == 0 // "ring" loops back to 0
array[4 % 3] == 1
array[5 % 3] == 2
从数学上讲,它是除法的余数

10 / 4    # (4 * 2) + 2
10 % 4             == 2

17 / 2    # (2 * 8) + 1
17 % 2             == 1