不能通过check50清除凯撒密码,无法找出原因

时间:2020-06-07 21:54:37

标签: c cs50 caesar-cipher

我对作业进行了编码,并尽我所能。我可以传递除一个以外的所有Check50参数!救命??验证是正确的,但是当我运行调试器时,它开始在加密部分出现问题。谢谢!

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

int main(int arg, string argv[])
{

//KEY VALIDATION
if (arg != 2) //checks to make sure that arg is 2 (for # of arguments)
{
    printf("Usage: ./caesar key\n");
    return 1;
}

else if  (arg == 2)
{ // checks key for validity
    for(int i = 0, len = strlen(argv[1]); i < len; i++)
    {
        if (!isdigit(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    //int key = atoi(argv[1]);
}
//ciphering


int key = atoi(argv[1]);
string plain = get_string("plaintext: ");
int len_plain = strlen(plain);
string cipher = plain;

 for (int x = 0; x < len_plain; x++)
        {
            if (plain[x] >= 'a' && plain[x] <= 'z')
            {
                cipher[x] = ((plain[x] + key)%122);
                if (cipher[x]<97)
                {
                    cipher[x] = cipher[x] + 96;
                }
            }
            else if (plain[x] >= 'A' && plain[x] <= 'Z')
            {
                cipher[x] = ((plain[x] + key)%90);
                if (cipher[x] < 65)
                {
                    cipher[x] = cipher[x] + 64;
                }
            }
            else
            {
                cipher[x] = plain[x];
            }
        }
    printf("ciphertext: %s\n", cipher);
}

我不断得到的check50结果是

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
***:( encrypts "barfoo" as "onesbb" using 65 as key
output not valid ASCII text

1 个答案:

答案 0 :(得分:0)

username