不确定如何进行凯撒算法,无法打印出解密文本
Not sure how to proceed with Caesar's algorithm, cannot print out the deciphered text
我目前正在尝试使用密钥将明文转换为 ciphertext。
代码:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int atoi(string argv);
int main(int argc, string argv[])
{
int k = 0;
string key = argv[1];
if (argc == 2)
{
for (k = 0; k < strlen(key); k++)
{
if (!isdigit(key[k]))
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
else
{
printf("\nSucces!");
}
}
}
else
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
string plaintext = get_string("\nplaintext: ");
for (int i = 0; i < strlen(plaintext); i++)
{
char c = plaintext[i];
if (isalpha(c))
{
printf("%c", (c + key) % 26);
}
}
}
如果明文字符串中的字符是字母表中的字母,我希望它移动关键位置。
我需要应用以下公式:ci = (pi + k) % 26,但它对我不起作用。有人可以提示我做错了什么吗?
我收到以下错误:
error: invalid operands to binary expression ('string' (aka 'char *') and 'int')
这个(c + key)
是引发错误的二进制表达式。 c
是一个字符(从技术上讲是 int
)。 key
是一个字符串,在此处声明和初始化 string key = argv[1];
。 stdlib.c
库中有一个名为 atoi
的函数,因此声明您自己的函数是一个有问题的决定。该库需要 include
才能使用 atoi 函数。那就是可用于将 key
转换为整数的函数。
我目前正在尝试使用密钥将明文转换为 ciphertext。 代码:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int atoi(string argv);
int main(int argc, string argv[])
{
int k = 0;
string key = argv[1];
if (argc == 2)
{
for (k = 0; k < strlen(key); k++)
{
if (!isdigit(key[k]))
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
else
{
printf("\nSucces!");
}
}
}
else
{
printf("\nUsage: %s key\n", argv[0]);
return 1;
}
string plaintext = get_string("\nplaintext: ");
for (int i = 0; i < strlen(plaintext); i++)
{
char c = plaintext[i];
if (isalpha(c))
{
printf("%c", (c + key) % 26);
}
}
}
如果明文字符串中的字符是字母表中的字母,我希望它移动关键位置。
我需要应用以下公式:ci = (pi + k) % 26,但它对我不起作用。有人可以提示我做错了什么吗?
我收到以下错误:
error: invalid operands to binary expression ('string' (aka 'char *') and 'int')
这个(c + key)
是引发错误的二进制表达式。 c
是一个字符(从技术上讲是 int
)。 key
是一个字符串,在此处声明和初始化 string key = argv[1];
。 stdlib.c
库中有一个名为 atoi
的函数,因此声明您自己的函数是一个有问题的决定。该库需要 include
才能使用 atoi 函数。那就是可用于将 key
转换为整数的函数。