-1

使用 CS50 库,这是我的代码,但它一直给我一个分段错误。我试图弄清楚它在哪里一直在做分段错误,并且由于某种原因它与我的功能有关。为什么它一直这样做?

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

string ciphertext(string plaintext[1], string key[1]);

int main(int argc, string argv[])
{
    // Variables
    string plaintext2[1];

    if (argc == 2)
    {
        if(strlen(argv[1]) == 26)
        {
            string text = get_string("plaintext: ");
            plaintext2[0] = text;
        }
        else
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
    }
    else
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    string ciphertext2 = ciphertext(&plaintext2[0], &argv[1]);
    printf("ciphertext: %s\n", ciphertext2);
}

string ciphertext(string plaintext[1], string key[1])
{
    // Variables
    string ciphertext[1];

    for (int i = 0; i < strlen(plaintext[0]); i++)
    {
        if (plaintext[0][i] == 'a')
        {
            ciphertext[0][i] = tolower(key[0][0]);
        }
        else if (plaintext[0][i] == 'A')
        {
            ciphertext[0][i] = toupper(key[0][0]);
        }
        else if (plaintext[0][i] == 'b')
        {
            ciphertext[0][i] = tolower(key[0][1]);
        }
        else if (plaintext[0][i] == 'B')
        {
            ciphertext[0][i] = toupper(key[0][1]);
        }
        else if (plaintext[0][i] == 'c')
        {
            ciphertext[0][i] = tolower(key[0][2]);
        }
        else if (plaintext[0][i] == 'C')
        {
            ciphertext[0][i] = toupper(key[0][2]);
        }
        else if (plaintext[0][i] == 'd')
        {
            ciphertext[0][i] = tolower(key[0][3]);
        }
        else if (plaintext[0][i] == 'D')
        {
            ciphertext[0][i] = toupper(key[0][3]);
        }
        else if (plaintext[0][i] == 'e')
        {
            ciphertext[0][i] = tolower(key[0][4]);
        }
        else if (plaintext[0][i] == 'E')
        {
            ciphertext[0][i] = toupper(key[0][4]);
        }
        else if (plaintext[0][i] == 'f')
        {
            ciphertext[0][i] = tolower(key[0][5]);
        }
        else if (plaintext[0][i] == 'F')
        {
            ciphertext[0][i] = toupper(key[0][5]);
        }
        else if (plaintext[0][i] == 'g')
        {
            ciphertext[0][i] = tolower(key[0][6]);
        }
        else if (plaintext[0][i] == 'G')
        {
            ciphertext[0][i] = toupper(key[0][6]);
        }
        else if (plaintext[0][i] == 'h')
        {
            ciphertext[0][i] = tolower(key[0][7]);
        }
        else if (plaintext[0][i] == 'H')
        {
            ciphertext[0][i] = toupper(key[0][7]);
        }
        else if (plaintext[0][i] == 'i')
        {
            ciphertext[0][i] = tolower(key[0][8]);
        }
        else if (plaintext[0][i] == 'I')
        {
            ciphertext[0][i] = toupper(key[0][8]);
        }
        else if (plaintext[0][i] == 'j')
        {
            ciphertext[0][i] = tolower(key[0][9]);
        }
        else if (plaintext[0][i] == 'J')
        {
            ciphertext[0][i] = toupper(key[0][9]);
        }
        else if (plaintext[0][i] == 'k')
        {
            ciphertext[0][i] = tolower(key[0][10]);
        }
        else if (plaintext[0][i] == 'K')
        {
            ciphertext[0][i] = toupper(key[0][10]);
        }
        else if (plaintext[0][i] == 'l')
        {
            ciphertext[0][i] = tolower(key[0][11]);
        }
        else if (plaintext[0][i] == 'L')
        {
            ciphertext[0][i] = toupper(key[0][11]);
        }
        else if (plaintext[0][i] == 'm')
        {
            ciphertext[0][i] = tolower(key[0][12]);
        }
        else if (plaintext[0][i] == 'M')
        {
            ciphertext[0][i] = toupper(key[0][12]);
        }
        else if (plaintext[0][i] == 'n')
        {
            ciphertext[0][i] = tolower(key[0][13]);
        }
        else if (plaintext[0][i] == 'N')
        {
            ciphertext[0][i] = toupper(key[0][13]);
        }
        else if (plaintext[0][i] == 'o')
        {
            ciphertext[0][i] = tolower(key[0][14]);
        }
        else if (plaintext[0][i] == 'O')
        {
            ciphertext[0][i] = toupper(key[0][14]);
        }
        else if (plaintext[0][i] == 'p')
        {
            ciphertext[0][i] = tolower(key[0][15]);
        }
        else if (plaintext[0][i] == 'P')
        {
            ciphertext[0][i] = toupper(key[0][15]);
        }
        else if (plaintext[0][i] == 'q')
        {
            ciphertext[0][i] = tolower(key[0][16]);
        }
        else if (plaintext[0][i] == 'Q')
        {
            ciphertext[0][i] = toupper(key[0][16]);
        }
        else if (plaintext[0][i] == 'r')
        {
            ciphertext[0][i] = tolower(key[0][17]);
        }
        else if (plaintext[0][i] == 'R')
        {
            ciphertext[0][i] = toupper(key[0][17]);
        }
        else if (plaintext[0][i] == 's')
        {
            ciphertext[0][i] = tolower(key[0][18]);
        }
        else if (plaintext[0][i] == 'S')
        {
            ciphertext[0][i] = toupper(key[0][18]);
        }
        else if (plaintext[0][i] == 't')
        {
            ciphertext[0][i] = tolower(key[0][19]);
        }
        else if (plaintext[0][i] == 'T')
        {
            ciphertext[0][i] = toupper(key[0][19]);
        }
        else if (plaintext[0][i] == 'u')
        {
            ciphertext[0][i] = tolower(key[0][20]);
        }
        else if (plaintext[0][i] == 'U')
        {
            ciphertext[0][i] = toupper(key[0][20]);
        }
        else if (plaintext[0][i] == 'v')
        {
            ciphertext[0][i] = tolower(key[0][21]);
        }
        else if (plaintext[0][i] == 'V')
        {
            ciphertext[0][i] = toupper(key[0][21]);
        }
        else if (plaintext[0][i] == 'w')
        {
            ciphertext[0][i] = tolower(key[0][22]);
        }
        else if (plaintext[0][i] == 'W')
        {
            ciphertext[0][i] = toupper(key[0][22]);
        }
        else if (plaintext[0][i] == 'x')
        {
            ciphertext[0][i] = tolower(key[0][23]);
        }
        else if (plaintext[0][i] == 'X')
        {
            ciphertext[0][i] = toupper(key[0][23]);
        }
        else if (plaintext[0][i] == 'y')
        {
            ciphertext[0][i] = tolower(key[0][24]);
        }
        else if (plaintext[0][i] == 'Y')
        {
            ciphertext[0][i] = toupper(key[0][24]);
        }
        else if (plaintext[0][i] == 'z')
        {
            ciphertext[0][i] = tolower(key[0][25]);
        }
        else if (plaintext[0][i] == 'Z')
        {
            ciphertext[0][i] = toupper(key[0][25]);
        }
        else
        {
            ciphertext[0][i] = plaintext[0][i];
        }
    }
    return ciphertext[0];
}

我真的需要一些帮助,我不知道为什么它总是出错。

4

2 回答 2

1

数组

string ciphertext[1];

未初始化并且具有不确定的值。

像这样访问这样的数组

ciphertext[0][i] = tolower(key[0][0]);

导致未定义的行为。

您需要分配一个大小等于表达式值的字符数组

strlen( *plaintext ) + 1

请注意,您可以使用大量 if-else 语句来定义字符串文字,例如

string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

然后你可以使用标准的字符串函数strchr,比如

char *p = strchr( letters, toupper( ( unsigned char)plaintext[0][i] ) );
if ( p != NULL )
{
    if ( plaintext[0][i] == *p )
    {
        ciphertext[0][i] = key[0][p - letters]);
    }
    else
    {
        ciphertext[0][i] = tolower(key[0][p - letters]);
    }
}
于 2020-07-23T21:47:51.683 回答
0

您已经有了答案(来自莫斯科的@Vlad),但我想指出几点:

  1. 复制是一种非常糟糕的做法,它不可读,更重要的是非常耗时。如果您看到“100”else if()条语句一个接一个,您应该怀疑您的代码存在严重错误。

  2. 尽量避免在代码中使用数字。如果您想更改已经复制粘贴一百次的值怎么办?我建议您不要使用数字,而是使用变量和符号常量

尝试改进您当前的代码,您可以编写如下内容:

for (int i = 0; i < strlen(plaintext[0]); i++)
{
    int ind = 0;
    int lower_case = 97, upper_case = 65;
    
    if (plaintext[0][i] == lower_case)
    {
        ciphertext[0][i] = tolower(key[0][ind]);
    }
    else if (plaintext[0][i] == upper_case)
    {
        ciphertext[0][i] = toupper(key[0][ind]);
    }
    
    ind++; lower_case++; upper_case++;
}
于 2020-07-23T21:51:07.243 回答