Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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
CS50 caesar.c-超出ASCII码范围的密文打印_C_Cs50 - Fatal编程技术网

CS50 caesar.c-超出ASCII码范围的密文打印

CS50 caesar.c-超出ASCII码范围的密文打印,c,cs50,C,Cs50,我正在处理凯撒问题的CS50(2021x版本),遇到了问题。我的程序在ASCII范围外打印(感谢curiouskiwi对discord的提示)。错误信息是::(使用23作为密钥将“barfoo”加密为“yxocll”,输出无效的ASCII文本)。我遇到的另一个问题是“world,say hello!”,原因相同(无效的ASCII文本)。其他的加密很好 我已经通过调试器,发现'letter'变量有时会变成一个负整数,如-119'/211',但我不明白为什么会这样。我希望看到与ASCII字母表相关的

我正在处理凯撒问题的CS50(2021x版本),遇到了问题。我的程序在ASCII范围外打印(感谢curiouskiwi对discord的提示)。错误信息是::(使用23作为密钥将“barfoo”加密为“yxocll”,输出无效的ASCII文本)。我遇到的另一个问题是“world,say hello!”,原因相同(无效的ASCII文本)。其他的加密很好

我已经通过调试器,发现'letter'变量有时会变成一个负整数,如-119'/211',但我不明白为什么会这样。我希望看到与ASCII字母表相关的正值。当这种情况发生时,字母将停止在控制台上打印

如果我键入./caesar 23 | cat-A,然后以明文形式给barfoo,则密码文本将显示为yxM-^IcM-^FM-^F$

int main(int argc, string argv[])
{
    // only 1 arugment, and positive argument only
    if (argc == 2 && argv[1] > 0)
    {
        // check if each char of argument is digit
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isdigit(argv[1][i]))
            {
                // do nothing
            }
            else
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }

        // change the key to how much letters should move over
        int input = atoi(argv[1]);
        int key = input % 26;
        char letter;

        // get the input
        string text = get_string("plaintext: ");
        printf("ciphertext: ");

        for (int i = 0, n = strlen(text); i < n; i++)
        {
            if (isalpha(text[i])) // if it is an alphabet
            {
                if (islower(text[i])) // if it is lowercase
                {
                    letter = text[i] + key; // add key to text[i]
                    if (letter > 122)
                    {
                        // loop around the alphabet
                        letter -= 26;
                    }
                    printf("%c", letter);
                }
                else // if it is uppercase
                {
                    letter = text[i] + key; // add key to text[i]
                    if (letter > 90)
                    {
                        // loop around the alphabet
                        letter -= 26;
                    }
                    printf("%c", letter);
                }
            }
            else // if it is not an alphabet
            {
                printf("%c", text[i]);
            }
        }
        printf("\n");
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}
intmain(intargc,字符串argv[])
{
//只有1个分段,只有正参数
如果(argc==2&&argv[1]>0)
{
//检查参数的每个字符是否为数字
对于(inti=0,n=strlen(argv[1]);i122)
{
//绕字母表循环
字母-=26;
}
printf(“%c”,字母);
}
else//如果是大写
{
字母=文本[i]+键;//将键添加到文本[i]
如果(字母>90)
{
//绕字母表循环
字母-=26;
}
printf(“%c”,字母);
}
}
else//如果不是字母表
{
printf(“%c”,文本[i]);
}
}
printf(“\n”);
}
其他的
{
printf(“用法:./caesar key\n”);
返回1;
}
}

您应该使用
int-letter
而不是
char-letter

这是因为

letter = text[i] + key;

将溢出带符号的
char
,例如,
'z'+23
argv[1]>0
不是检查NULL或字符串长度的正确方法,只需检查
argc
的值就足够了,您不需要在
islower()
isupper()
.b)之前检查
isalpha()不要硬编码魔法数字,用
'Z'
代替
90
。对不起,我想我写的代码不清楚。在这一行,我试图检查用户是否输入了非负数。有没有更好的方法写它呢?3)字母=文本[i]+键
应该是
if(isupper(text[i]){letter=(text[i]-'A'+键)%26+'A';}
等,因为键可能是26的许多倍数。您需要首先执行
atoi
,然后验证
input
是否为非负数。