【发布时间】:2015-11-01 06:48:48
【问题描述】:
我在使用 Vigenere 时遇到了一点问题,需要一些帮助。
/*
This program is a Vigenere Cipher.
I am Daniel of Asguard.
*/
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
string cipher; //this is a placeholder for the ciphered message.
char * key = argv[1];
int i = 0;
if (argc != 2) //this is meant to trigger if you don't enter the right call. So
{
printf("Please enter the cipher key when you call the program, such as './CaesarCipher 7'.\n"); //
return 1;
}
if (!isalpha(key[i])) //this is meant to trigger if you don't enter the right call. So
{
printf("Please only enter a word, no numerical numbers please."); //
return 1;
}
do
{
//printf("Please enter the message you would like to have converted, please. \n");
cipher = GetString();
}
while (cipher == NULL);
for (int i = 0, k = 0, n = strlen(cipher); i < n; i++, k++) //this is so the code knows to change only the characters in the sting cipher.
{
if (k >= strlen(key))
{
k = 0;
}
{
if (isupper(cipher[i]))
{
//cipher[i] = 'A' + (((cipher[i] - 'A') + (key[k]) - 'A') % 26);
cipher[i] = ((key[k] - 65) + (cipher[i] - 65)) % 26;
printf("%s\n", cipher);
}
else (islower(cipher[i]));
{
//cipher[i] = 'a' + (((cipher[i] - 'a') + (key[i]) - 'a') % 26);
cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26;
printf("%s\n", cipher);
}
}
}
printf("%s\n", cipher);
return 0;
}
当我这样做时,我的结果会得到奇怪的字符:⎽c▒⎺e┼├⎼▒┤└e⎼@☃de5▮:·/┬⎺⎼┐⎽⎻▒ce/⎻⎽e├2 $ └▒ ┐e ┴☃±e┼e⎼e 完成后我终端中的所有字母。
BaZ 的结果最终看起来像这样:
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- 注意事项
- f 注意
- f 注意
- 注意
- 特
- 特
- 特
- ├e
- e
【问题讨论】:
标签: c encryption vigenere cs50