【发布时间】:2015-11-20 22:32:47
【问题描述】:
我将 Vigenere Cipher 编码为 CS50 的一部分。这是我的代码。
#include<cs50.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("Bad Argument!\n");
return 1;
}
for(int k = 0; k <= strlen(argv[1]) - 1; k++)
{
if (isalpha(argv[1][k]) == 0)
{
printf("Bad Argument!\n");
return 1;
}
}
string s = GetString();
char a[strlen(s)];
int i, j = 0;
int l = strlen(argv[1]);
for (i = 0; i < strlen(s); i++)
{
int k = (int)(tolower(argv[1][j%l]) - 'a');
if (s[i] >= 'A' && s[i] <= 'Z')
{
a[i] = (s[i] - 'A' + k) % 26 + 'A';
j++;
}
else if (s[i] >= 'a' && s[i] <= 'z')
{
a[i] = s[i] - 'a' + k) % 26 + 'a';
j++;
}
else
a[i] = s[i];
}
printf("%s\n", a);
}
这是我的 pset2 vigenere.c 代码。但是,一旦我编译并运行它,我会在密文末尾得到其他字符,例如:
因此,Check50 在某些情况下会接受答案,而在其他情况下则不会。
:( encrypts "a" as "a" using "a" as keyword
\ expected output, but not "a\u001c������\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
\ expected output, but not "xoqmd, rby gflkp!v��\t��\n"
:) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
我做错了什么?
【问题讨论】:
标签: c encryption vigenere cs50