cs50 凯撒,我不明白检查 50 失败



下面是我的代码,它适用于10/11检查,但显示了此检查的错误消息::(使用12作为密钥将"世界,打招呼!"加密为"iadxp,emk tqxxa!"原因预期的";密文:ia…";,而不是";密文:ia…";

日志正在运行/凯撒12。。。发送输入世界,说你好!。。。检查输出";密文:iadxp,emk tqxxa!\n〃;。。。

预期输出:密文:iadxp,emk tqxxa!实际输出:密文:iadxp,emk tqxxa!

我不知道为什么它们看起来和我一模一样,我在这里遗漏了什么明显的错误??

#include <ctype.h> 
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(int key, string word[])
{
//Ensure only a key of 2 is accepted   
if (key != 2) 
{   
printf("Usage: ./caesar keyn");
return 1;
}
//Ensure the second input has has no letters.  
for (int i = 0, n = strlen(word[1]); i < n;)
if (isalpha(word[1][i]))
{
printf("Usage: ./caesar keyn");
return 1;
}
//Ensure each character of the input is a digit  
else if (isdigit(word[1][i]))
{
i++;
}
else
{
printf("Usage: ./caesar keyn");
return 1;
}

string text = get_string("Plain text: ");
{
printf("ciphertext: ");
}
{  
//check each character is an alphabetical one
for (int b = 0, n = strlen(text); b < n; b++;)
{ 
int k = 0;
int cipher = 0;

if(!isalpha(text[b]))

{  
printf("%c", text[b]);
}   

else if (isalpha(text[b]))
{
//Calculate the cipher code for a lower-case letter        
if (islower(text [b]))
{ 
int t = text [b] - 97;
k = (t + atoi(word[1])) % (26);
cipher = k + 97;
//Calculate the cipher code for an upper-case letter              
}
else if (isupper(text[b]))

{
int t = text [b] - 65;
k = (t + atoi(word[1])) % (26);
cipher = k + 65;
}
}



//print cipher text.       
{
printf("%c", cipher);
}
}

printf("n");

} 

}

首先,要查看check50看到了什么,请对失败的输入进行此操作。

./caeser 12 | cat -v

cat -v显示不可打印的字符。无法打印的字符来自这里printf("%c", cipher);。该行执行输入(text[b](是否为alpha。该行需要在else if (isalpha(text[b]))块内移动。

最新更新