我正在为我的实验表制作凯撒密码,并使其能够加密 3 个替代密码(凯撒密码),这是练习的重点。但是有一件事困扰着我。首先,如果我把它放在 3 以外的地方,就会有一个尾随字符。例如,输入“恶意软件”,然后输入 2 作为密钥。这是我的代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char text[100];
int key,i;
printf("Please enter a word/sentence (lowercaps) for encrypting :\n ");
fgets(text,100,stdin);
printf("Please enter the key that you desire : eg:14\n");
scanf("%d", &key);
for(i=0;i<strlen(text);i++)
{
if (key>=26)
{
key=key%26;
}
if (text[i]==' ')
{
continue;
}
if(text[i]+key>'z')
{
text[i]-=97;
text[i]+=26;
text[i]+=key;
text[i]%=26;
text[i]+=97;
}
else
{
text[i]=text[i]+key;
}
}
printf("this is your encrypted text : %s", text );
}
我希望我遵循正确的缩进方法进行编码。也因此被很多人嫌弃