【发布时间】:2021-03-24 00:53:21
【问题描述】:
挑战是创建一个程序,您可以在其中输入一个 26 字符长的字符串,它应该基本上替换字母表。因此,例如,如果您将密钥设置为“qwertyuiopasdfghjklzxcvbnm”,它将使用 q 表示 a,w 表示 b,e 表示 c,r 表示 d 等。然后您输入一个字符串作为您的消息,它将明文转换为密文。
到目前为止,在课程中我们还没有涉及指针,所以我想人们应该能够在不使用 * 或 & 引用指针的情况下完成这一挑战。相反,我们被告知使用允许我们使用“字符串”的库,而该库处理在后台为我们使用指针的技术工作。不过我对此有点困惑,因为我最初尝试的代码是这样的:
{
if(isupper(plainText[i]))
{
cypherText[i] = key[plainText[i] - 'A'];
}
else if (islower(plainText[i]))
{
cypherText[i] = key[plainText[i] - 'a'];
}
}
printf("%s\n", cypherText);
但我收到以下错误:
error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'char'; take the address with & [-Werror,-Wint-conversion]
cypherText[i] = key[plainText[i] - 'A'];
^ ~~~~~~~~~~~~~~~~~~~~~~~
&
error: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'char'; take the address with & [-Werror,-Wint-conversion]
cypherText[i] = key[plainText[i] - 'a'];
^ ~~~~~~~~~~~~~~~~~~~~~~~
&
error: format specifies type 'char *' but the argument has type 'string *' (aka 'char **') [-Werror,-Wformat]
printf("%s\n", cypherText);
所以我将代码更改为以下
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
string key;
do
{
key = get_string("Key:");
}
while(strlen(key) != 26);
string plainText = get_string("Text:");
string cypherText[strlen(plainText)];
for (int i = 0; plainText[i] != '\0'; i++)
{
if(isupper(plainText[i]))
{
cypherText[i] = &key[plainText[i] - 'A'];
}
else if (islower(plainText[i]))
{
cypherText[i] = &key[plainText[i] - 'a'];
}
}
printf("%s\n", *cypherText);
}
现在它可以编译但不能按预期工作,而且我无法通过调试来识别该错误。基本上,当我使用键“bcdefghijklmnopqrstuvwxyza”(字母左移一个字母)运行它,然后我使用“hello”之类的消息时,我将得到以正确字母开头的输出“ijklmnopqrstuvwxyza”,然后继续解析字母对我来说莫名其妙。即使我只写一个字母长的消息,密文也会打印为多个字符。
所以我的问题是:
我的假设是否正确,即有一种方法可以在不使用指针的情况下完成此挑战,如果是这样,我如何在不使用 & 或 * 的情况下更正这些错误消息?
当我使用 & 和 * 时,是什么导致了这种奇怪的尾随字母行为?
谢谢。
【问题讨论】:
-
string cypherText[strlen(plainText)];声明了一个string数组,您需要一个char数组。如果您打算在最后一行代码中使用char的数组作为string,请记住分配一个额外的字符并将0分配给它以终止字符串。
标签: arrays c pointers cs50 c-strings