【问题标题】:Array comparison and Loops In CC中的数组比较和循环
【发布时间】:2014-03-21 01:35:15
【问题描述】:

我正在学习这个免费的在线课程,因此资源和帮助非常有限。他们想要一个 Vigenere 密码。我的代码正在通过所有测试,我认为它已经完成,直到我输入“ho1W are y0Ou?”作为文本和“heLLo”作为它们的键。执行是完美的,除了小写的 u 不会继续通过 'z' - 'a'循环,而是打印'''。代码确实在“how”中的“W”和“you”中的“y”中成功执行了“z”到“a”循环。关键,“heLLo”是确实重复成功,并且当它碰到'u'时不在strlen的末尾。非字母字符也不会增加1。我不知道从这一点开始。任何人都可以提供一些建议?谢谢!

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

    // Function to get string (text) from user
string Encrypt(void);

int main(int argc, string argv[])
{
    // Exits with improper arguement count
    if (argc != 2)
    {
        printf("You must enter one keyword when running the program.\n");
        return 1;
    }
    // Sets key entered for command argument 1
    string key = argv[1];

    // Checks key to make sure a-z is entered. Exits if not.
    for (int i = 0, word = strlen(key); i < word; i++)
    {
        if (isalpha(key[i]))
        {
        }
        else
        {
            printf("Only letters are allowed for the key.\n");
            return 1;
        }

    }

    string text = Encrypt();

    // Secret used to print out final message
    char secret = 'a';

    // K contorls array place in key
    int k = 0;

    // If text is entered and alpha: compares text[i] and key[k]
    if (text != NULL)
    {
        for (int i = 0, len = strlen(text); i < len; i++)
        {
            if (isalpha(text[i]))
            {
                // Checks k poition to make sure it is within array
                if (k == strlen(key))
                {
                    k = 0;
                }

                // Converts key if text is lowercase
                if (islower(text[i]))
                {
                    secret = (((text[i] - 'a') + (key[k] - 'a')) % 26) + 'a';
                    printf("%c", tolower(secret));
                }

                // Converts key if text is uppercase
                if (isupper(text[i]))
                {
                    secret = (((text[i] - 'A') + (key[k] - 'A')) % 26) + 'A';
                    printf("%c", toupper(secret));
                }

                k++;
            }

            // If not alpha ignores loop and prints text char.
            else
            {
                printf("%c", text[i]);
            }
        }
    }

    return 0;
}

string Encrypt(void)
{
    printf("Enter your text.");
    string text = GetString();

    return text;
}

【问题讨论】:

  • @JoachimPileborg char*
  • 不要让if else 带有用于isalpha 检查的空“真”分支,为什么不只是if (!isalpha(...)) { ...; return 1; }?无论如何,这就是编译器将如何优化它的方式。
  • 更重要的是,GetString 是什么?它返回的指针是如何创建的?
  • @JoachimPileborg 堆。

标签: c arrays cs50


【解决方案1】:

问题是当它到达字符串中的“u”时,您在键中的“L”上。所以当这段代码运行时:

secret = (((text[i] - 'a') + (key[k] - 'a')) % 26) + 'a';

通过替换你有:

secret = ((('u' - 'a') +  ('L' - 'a')) % 26) + 'a';

提示:'L' - 'a' = -21。 'u' - 'a' = 20。希望你能从这里解决剩下的问题,祝你好运。

【讨论】:

  • 谢谢!在意识到错误后,我能够解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
相关资源
最近更新 更多