【问题标题】:Vigenere CS50 - Need help cycling through alpha lettersVigenere CS50 - 需要帮助循环字母
【发布时间】:2018-06-17 09:30:01
【问题描述】:

我正在尝试CS50 Vigenere exercise

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

int main(int argc, string argv[])
{
//Check for 2 command line arguments
if (argc != 2)
{
    printf("Nah bro, you gotta have 2 arguments.\n");
    return 1;
}
//Check is alpha
else {
    for (int i = 0; i < strlen(argv[1]); i++)
    {
        if (isalpha(argv[1][i]) == 0)
        {
            printf("Nah bro, u gots to use letters.\n");
            return 1;
        }
    }
}


//Prompt user to input text
    printf("plaintext: ");
    string p = get_string();

//Cipher
    printf("ciphertext: ");
    string k = argv[1];
    int cipherlen = strlen(k);



//Cycle through key letters
for (int i = 0, j = 0, n = strlen(p); i < n; i++)
{


    if (isalpha(p[i]))
    {

        if (isupper(p[i]))
            {
            printf("%c", ((p[i] - 65) + (k[(j % cipherlen)]) - 65) % 26 + 65);
            j++;
            }


        else if (islower(p[i]))
            {
            printf("%c", ((p[i] - 97) + (k[(j % cipherlen)]) - 97) % 26 + 97);
            j++;
            }


        else
            printf ("%c", p[i]);
    }

    }
        printf("\n");
        return 0;
}

根据检查,这是我的错误代码:

https://cs50.me/checks/a56bc9325327035cb0e8d831693c9805c4b6468b

我知道我的问题与循环遍历每个字母有关,但没有将其应用于空格或符号。我尝试使用 if (isalpha) 语句和 else printf(" ") 但它不适用于数字或符号。我认为添加 j++ 只会遍历字母字符,但它似乎没有帮助。

这里有什么我错过的超级简单的东西吗?

【问题讨论】:

  • "string" 好像是"char *" 类型,所以应该没问题...
  • @melpomene 它应该适用于 ascii 代码
  • @LeoH 你应该使用isprint() 而不是isalpha() 来输出"$! 等字符。

标签: c cs50 vigenere


【解决方案1】:

您的代码的基本结构看起来不错。

我发现它存在三个问题:

  1. 您的所有printfs 都受到if (isalpha(p[i])) 检查的保护,因此如果明文字符不是字母,您的程序永远不会输出任何内容(它应该输出不变的字符)。解决这个问题很简单;只需删除循环中的外部if (...)

    for (int i = 0, j = 0, n = strlen(p); i < n; i++)
    {
        if (isupper(p[i]))
            {
            printf("%c", ((p[i] - 65) + (k[(j % cipherlen)]) - 65) % 26 + 65);
            j++;
            }
        else if (islower(p[i]))
            {
            printf("%c", ((p[i] - 97) + (k[(j % cipherlen)]) - 97) % 26 + 97);
            j++;
            }
        else
            printf ("%c", p[i]);
    }
    

    内部if/else if 链正确处理这种情况。

  2. 当前明文字符p[i]和当前关键字字符k[j % cipherlen]可以独立大写/小写。您的代码目前根本不处理这个;相反,它假定如果p[i] 是大写,k[j % cipherlen] 也必须是大写,小写也是如此。

    顺便说一句,我建议不要在代码中写6597。我会分别使用 'A''a' 代替,这使得事情更具可读性恕我直言。

    要解决此问题,您必须分别测试 k[j % cipherlen] 的大写/小写。例如:

    for (int i = 0, j = 0, n = strlen(p); i < n; i++)
    {
        char key_char = k[j % cipherlen];
        int key_shift;
        if (isupper(key_char)) {
            key_shift = key_char - 'A';
        } else {
            key_shift = key_char - 'a';
        }
        if (isupper(p[i]))
            {
            printf("%c", ((p[i] - 'A') + key_shift) % 26 + 'A');
            j++;
            }
        else if (islower(p[i]))
            {
            printf("%c", ((p[i] - 'a') + key_shift) % 26 + 'a');
            j++;
            }
        else
            printf ("%c", p[i]);
    }
    

    (我厌倦了重复输入相同的表达式,所以我将公共位提取到变量中(key_charkey_shift)。这里唯一棘手的部分是 j 应该只在 @987654340 时递增实际使用了@,但您的代码已经处理了它。)

  3. 这是一个微妙的点,但如果参数为负,所有&lt;ctype.h&gt; 函数(例如isupperisalpha、...)都有未定义的行为。 char 在许多实现中都是有符号类型,因此随机字符 str[i] 很可能是负数。为了完全可移植和正确,您应该在每个此类调用中将字符转换为 (unsigned char)

    if (isupper((unsigned char)key_char))
        ...
    if (isupper((unsigned char)p[i]))
        ...
    else if (islower((unsigned char)p[i]))
        ...
    

    或者,完全接受 ASCII(您的代码的其余部分已经假定它)并执行:

    if (key_char >= 'A' && key_char <= 'Z')
        ...
    if (p[i] >= 'A' && p[i] <= 'Z')
        ...
    else if (p[i] >= 'a' && p[i] <= 'z')
        ...
    

【讨论】:

  • 哇。非常感谢。你完全帮助我以新的眼光看待事物。
  • 问题虽然 - 我们有什么理由使用 key_shift = key_char - 'a' 而不是说 tolower()?
猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 2020-06-18
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多