【发布时间】:2015-10-30 22:16:47
【问题描述】:
我正在用 C 语言为 CS50 编写一个 Vigenere 密码程序,它几乎可以完美运行。错误是有时当加密环绕输出时是一个?白色圆圈中的符号。
下面是我的代码;
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
char a;
char letter;
char cryptletter;
char passletter;
string message;
int messcount = 0;
if(argc != 2)
{
printf("Command line must contain one passphrase\n");
return 1;
}
for(int i=0; i < strlen(argv[1]); i++)
{
a = argv[1][i];
if(isalpha(a) == 0)
{
printf("Encryption keyphrase must only contain alphabetic characters\n");
return 1;
}
}
message = GetString();
for (int j = 0; j < strlen(message); j++)
{
letter = message[j];
if(passletter < 91 && passletter > 64)
{
passletter = passletter + 32;
}
if(isupper(letter) != 0) //if uppercase 65-90
{
passletter = argv[1][messcount];
cryptletter = passletter + letter - 97;
if(cryptletter > 90)
{
cryptletter = cryptletter - 25;
}
printf("%c", cryptletter);
messcount++;
}
if(islower(letter) != 0) //if lowercase 97-122
{
passletter = argv[1][messcount];
cryptletter = passletter + letter - 97;
if(cryptletter > 122)
{
cryptletter = cryptletter - 25;
}
printf("%c", cryptletter);
messcount++;
}
if(messcount > strlen(argv[1])-1)
{
messcount = 0;
}
if (isalpha(letter) == 0)
{
printf("%c", letter);
}
}
printf("\n");
}
如果凯撒密码存在类似问题,我们将不胜感激。谢谢
【问题讨论】: