为什么我在这个凯撒密码的实现中得到一个空值?

问题描述 投票:0回答:1

我已经在 C 中实现了 Caesar 的密码,尽管算法有效,但我不明白为什么(有时)如果我在添加密钥之前不减去字母表的第一个字母,我会得到一个空值.这是完整的代码(参见第 59 行或搜索

return (letter + k) % 26
):

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

string caesar_cipher(string text, int k);
char replace_letter(char letter, int k);
bool is_numeric(string input);

int main(int argc, string argv[])
{
    if (argc != 2 || (argc == 2 && !is_numeric(argv[1])))
    {
        fprintf(stderr, "You must specify a key to the cipher! Exiting...\n");
        exit(EXIT_FAILURE);
    }

    // Convert command line argument to integer.
    int k = atoi(argv[1]);

    // Prompts user for the text to encrypt
    string text = get_string("plaintext: ");

    // Returns encrypted text
    printf("ciphertext: %s\n", caesar_cipher(text, k));

    exit(EXIT_SUCCESS);
}

string caesar_cipher(string text, int k)
{
    int text_length = strlen(text);
    string ciphered_text = text;

    for (int i = 0; text[i] != '\0'; i++)
    {
        ciphered_text[i] = replace_letter(text[i], k);
    }

    return ciphered_text;
}

char replace_letter(char letter, int k)
{
    // Early return when 'letter' is a non-alphabetical character
    if (!isalpha(letter))
    {
        return letter;
    }

    char operation_letter = 'a';

    if (isupper(letter))
    {
        operation_letter = 'A';
    }

    // return (letter + k) % 26; // Sometimes, returns an empty value
    return ((letter - operation_letter + k) % 26) + operation_letter;
}

// Loop over characters to check if each one of them is numeric
bool is_numeric(string input)
{
    for (int i = 0; input[i] != '\0'; i++)
    {
        // If character is not numeric
        // returns false.
        if (isdigit(input[i]) == 0)
        {
            return false;
        }
    }

    return true;
}

谁能解释为什么会这样?

c cs50 caesar-cipher ansi-c
1个回答
1
投票

您需要在函数中考虑字母表的第一个字母(

a
A
),因为
char
s 在内部表示为整数(通常只有一个字节,但这取决于编码).例如,在 ASCII 中,执行
% 26
将导致 ASCII 表的前 26 个值中的任何一个,其中没有一个是实际字母。希望我说清楚了。

© www.soinside.com 2019 - 2024. All rights reserved.