我一直在对此代码的循环部分进行更改,而我的check50始终失败。我不知道这是怎么回事。下面是我的代码:

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

int main(int argc, string argv[])
{
    // declare variables
    int cipherText;

    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword");
        printf("\n");
        return 1;
    }
    // keyword is the second command line argument
    string key = argv[1];
    int keylen = strlen(argv[1]);

    // iterate through keyword to check if alphabetical
    for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if ((key[i] >= '0') && (key[i] <= '9'))
        {
            printf("Keyword must consist only of letters.");
            return 1;
        }
    }

    // get the plaintext
    string plainText = GetString();

    // encypher - iterate over the characters in string, print each one encrypted
    for (int i = 0, j = 0, n = strlen(plainText); i < n; i++, j++)
    {
        // start the key again if key shorter than plainText
        if (j >= strlen(key))
        {
            j = 0;
        }

        // skip key[j] if plainText[i] is not an alpha character
        if (!isalpha(plainText[i]))
        {
            j = (j-1);
        }

        // makes Aa = 0, Zz = 25 for the uppercase letters
        if (isupper(key[j]))
        {
            key[j] = (key[j] - 'A');
        }

        // makes Aa = 0, Zz = 25 for lowercase letters
        else if (islower(key[j]))
        {
            key[j] = (key[j] - 'a');
        }


        if (isupper(plainText[i]))
        {
            cipherText = (plainText[i] - 'A');
            cipherText = ((cipherText + key[j%keylen])%26) + 'A';
            printf("%c", cipherText);
        }

        else if (islower(plainText[i]))
        {
            cipherText = (plainText[i] - 'a');
            cipherText = ((cipherText + key[j%keylen])%26 + 'a');
            printf("%c", cipherText);
        }

        else
        {
            printf("%c", plainText[i]);
        }
    }
    printf("\n");
    return 0;

}


有人回答:“第一个for循环有问题。条件是在检查i > keylen时应检查i < keylen”。

同样,在计算下一个输出值时,步骤应为


(p [i] -65)得出0到25之间的数字
加(key [i%keylen])会产生0到50之间的数字
应用模26,因此数字在0到25之间(这是缺少的步骤)
然后加65以得到输出”


这就是我试图做的。

最佳答案

给出以下代码:

int keylen = strlen(argv[1]);

// iterate through keyword to check if alphabetical
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
    if ((key[i] >= '0') && (key[i] <= '9'))
    {
        printf("Keyword must consist only of letters.");
        return 1;
    }
}


您在循环中进行的测试将数字标识为“不是字母”(有效),但忽略标点符号,空格等。您可能应该使用if (!isalpha(key[i]))进行测试(在错误消息中打印错误字符是有礼貌的,应该在标准错误而不是标准输出上打印错误字符,并以换行符结尾:

        fprintf(stderr, "Keyword must consist only of letters (%c found at %d)\n",
                key[i], i+1);


您可以对其进行优化,以便它不会尝试使用%c打印不可打印的字符,但这是朝着正确方向迈出的重要一步。

您真的不需要在循环中设置n。您只需在循环之前设置keylen,这样您就可以编写:

for (int i = 0; i < keylen; i++)


但是,这主要是化妆品。您的真正问题出在这里:

    // start the key again if key shorter than plainText
    if (j >= strlen(key))
    {
        j = 0;
    }

    // makes Aa = 0, Zz = 25 for the uppercase letters
    if (isupper(key[j]))
    {
        key[j] = (key[j] - 'A');
    }

    // makes Aa = 0, Zz = 25 for lowercase letters
    else if (islower(key[j]))
    {
        key[j] = (key[j] - 'a');
    }


您可以通过密钥在每次迭代中修改密钥字符串。但是,不幸的是,如果键中的任何字母是aA,则已将其转换为'\0',这意味着strlen(key)返回的答案与以前不同。因此,应使用keylen代替strlen()。 AFAICS,如果没有aA,那部分代码就可以了。

稍后,您将:

    if (isupper(plainText[i]))
    {
        cipherText = (plainText[i] - 'A');
        cipherText = ((cipherText + key[j%keylen])%26) + 'A';
        printf("%c", cipherText);
    }


j % keylen是多余的; j已被限制为0 .. keylen-1。与小写文本的代码类似。

将这些更改放在一起,并使用GetString()虚拟化fgets()函数,我得到:

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

typedef char *string;

static char *GetString(void)
{
    static char buffer[4096];
    if (fgets(buffer, sizeof(buffer), stdin) == 0)
    {
        fprintf(stderr, "EOF detected in GetString()\n");
        exit(EXIT_SUCCESS);
    }
    buffer[strlen(buffer) - 1] = '\0';
    return buffer;
}

int main(int argc, string argv[])
{
    // declare variables
    int cipherText;

    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword");
        printf("\n");
        return 1;
    }
    // keyword is the second command line argument
    string key = argv[1];
    int keylen = strlen(argv[1]);

    // iterate through keyword to check if alphabetical
    for (int i = 0; i < keylen; i++)
    {
        if (!isalpha(key[i]))
        {
            printf("Keyword must consist only of letters (%c at %d)\n",
                   key[i], i+1);
            return 1;
        }
    }

    // get the plaintext
    string plainText = GetString();

    // encypher - iterate over the characters in string, print each one encrypted
    for (int i = 0, j = 0, n = strlen(plainText); i < n; i++, j++)
    {
        // start the key again if key shorter than plainText
        if (j >= keylen)
        {
            j = 0;
        }

        // skip key[j] if plainText[i] is not an alpha character
        if (!isalpha(plainText[i]))
        {
            j = (j - 1);
        }

        // makes Aa = 0, Zz = 25 for the uppercase letters
        if (isupper(key[j]))
        {
            key[j] = (key[j] - 'A');
        }
        // makes Aa = 0, Zz = 25 for lowercase letters
        else if (islower(key[j]))
        {
            key[j] = (key[j] - 'a');
        }

        if (isupper(plainText[i]))
        {
            cipherText = (plainText[i] - 'A');
            cipherText = ((cipherText + key[j]) % 26) + 'A';
            printf("%c", cipherText);
        }
        else if (islower(plainText[i]))
        {
            cipherText = (plainText[i] - 'a');
            cipherText = ((cipherText + key[j]) % 26 + 'a');
            printf("%c", cipherText);
        }
        else
        {
            printf("%c", plainText[i]);
        }
    }
    printf("\n");
    return 0;
}


样品运行:

$ ./vigenere bakedalaska
What a wonderful world! The news is good, and the Vigenere cipher is solved.
Xhkx d wznvorguv arrwd! Lre oegw ls rogn, aod dlh Vtgwxese mmshpr ac splfig.
$

09-25 18:14