替换cs50在check50中输出一些错误,但代码在我的终端上工作正常

bprjcwpo  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(158)

因此,任务是将用户提示文本中的每个字符更改为给定的键。我从另一个人那里阅读了同样的问题,但没有解决方案。
以下是任务的链接:https://cs50.harvard.edu/x/2022/psets/2/substitution/
问题是,当我在我自己的终端上运行我的代码时,它会输出他们想要的正确结果,但是当我运行check50时,它失败了。
这是他们给我看的失败任务(我自己尝试时也会输出正确的结果):

:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: Z\...", not ""
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: z\...", not ""
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: NJ...", not ""
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: Ke...", not ""
:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
    expected "ciphertext: Cb...", not ""
:( encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
    expected "ciphertext: Cb...", not ""
:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
    expected "ciphertext: Cb...", not ""
:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
    expected "ciphertext: Rq...", not ""
:( does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
    expected "ciphertext: Yq...", not ""

这是我的密码

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

bool validate_command_line(int argc, string key);
string encrypt(string key, string txt);

int main(int argc, string argv[]) 
{
    // Validate command-line arguments
    if (!validate_command_line(argc, argv[1]))
    {
        return 1;
    }
    
    // Prompt user for a plaintext
    string plaintext = get_string("plaintext: ");
    
    // Encrypt plaintext to ciphertext using a key
    string ciphertext = encrypt(argv[1], plaintext);

    // Display ciphertext
    printf("ciphertext: %s\n", ciphertext);

    return 0;
}

// To encrypt plaintext to ciphertext by key
string encrypt(string key, string txt)
{
    // Get length of a user plaintext
    int l = strlen(txt);

    // This will store at which index key should access
    int index_letter = 0;

    // To store encrypted text
    char encrypted_text[l + 1];
    
    // Iterate throught plaintext by it's length
    for (int i = 0; i < l; i++)
    {
        // Make sure to only encrypt alphabet characters
        if (isalpha(txt[i]))
        {
            /*
                Handling lowercase character
                We substract current character to ASCII Code of a, which 97
                to get at index position value of the character
                Ex: a is 97-97 = 0 | b is 98-97 = 1
                Then use it to access at which key to access
            */
            if (islower(txt[i]))
            {
                index_letter = txt[i] - 97;
                encrypted_text[i] = tolower(key[index_letter]);
            }
            /* Handling uppercase character */
            else
            {
                index_letter = txt[i] - 65;
                encrypted_text[i] = toupper(key[index_letter]);
            }
        }
        // If it's other symbol, just copying it
        else
        {
            encrypted_text[i] = txt[i];
        }
    }

    // Store NUL character at the end of char array
    encrypted_text[l] = '\0';

    // Store it to string, because we can't return char array
    string ciphertext = encrypted_text;

    return ciphertext;
}

// To validate command-line arguments 
bool validate_command_line(int argc, string key)
{
    // Validate total of command-line arguments
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return false;
    }

    // Validate key contain 26 alphabet
    int key_length = strlen(key);
    if (key_length != 26)
    {
        printf("Key must contain 26 characters.\n");
        return false;
    }

    // Validate key should only alphabet AND not contain repeated characters
    int repeated_charaters[key_length];
    int index_character = 0;

    for (int i = 0; i < key_length; i++)
    {
        if (!isalpha(key[i]))
        {
            printf("Key must only contain alphabetic characters.\n");
            return false;
        }

        index_character = tolower(key[i]) - 97;
        if (repeated_charaters[index_character] == 1)
        {
            printf("Key must not contain repeated characters.\n");
            return false;
        }
        
        repeated_charaters[index_character]++;
    }

    return true;
}

string encrypt(string key, string txt)中所示
我创建了一个返回字符串的函数,因为我想打印main()中的文本。也许你会告诉我我不能返回字符串或字符数组
但是我把这个函数改成了void(),这样它就可以打印函数中的结果而不返回。
就像这样:

void encrypt(string key, string txt)
{
    // Get length of a user plaintext
    int l = strlen(txt);

    // This will store at which index key should access
    int index_letter = 0;

    // To store encrypted text
    char encrypted_text[l + 1];
    
    // Iterate throught plaintext by it's length
    for (int i = 0; i < l; i++)
    {
        // Make sure to only encrypt alphabet characters
        if (isalpha(txt[i]))
        {
            /*
                Handling lowercase character
                We substract current character to ASCII Code of a, which 97
                to get at index position value of the character
                Ex: a is 97-97 = 0 | b is 98-97 = 1
                Then use it to access at which key to access
            */
            if (islower(txt[i]))
            {
                index_letter = txt[i] - 97;
                encrypted_text[i] = tolower(key[index_letter]);
            }
            /* Handling uppercase character */
            else
            {
                index_letter = txt[i] - 65;
                encrypted_text[i] = toupper(key[index_letter]);
            }
        }
        // If it's other symbol, just copying it
        else
        {
            encrypted_text[i] = txt[i];
        }
    }

    // Store NUL character at the end of char array
    encrypted_text[l] = '\0';

    printf("%s", encrypted_text);
}

我对编程很陌生,希望你们能给予我解决方案,谢谢。

py49o6xq

py49o6xq1#

我的理解是string就是cs50中的char *

string ciphertext = encrypted_text;

return ciphertext;

无效,因为encrypted_text是将在函数结尾销毁的局部数组。不能返回指向局部数组的指针

nwlqm0z1

nwlqm0z12#

问题是数组repeated_charaters没有初始化。本地系统与check50系统的区别在于“未初始化”内存中的内容。在声明之后将所有array_elements设置为0以解决问题(在“This is my code”之后粘贴的代码中)

相关问题