我正在爲我的實驗表製作一個凱撒密碼,並且已經使它能夠加密3練習(Caesar's Cipher),這是練習的要點。但是有一件事情讓我煩惱。首先,如果我把它放在3以外,會有一個尾隨字符。例如,鍵入「malware」,鍵入2。 這是我的代碼:C語言:在我的輸出的最後一個字符後面有一個尾隨字符
#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);
}
我希望我也跟着編碼正確的縮進的方法。得到了很多,因爲那不喜歡的
你是什麼意思到尾隨字符? 尾隨的空格? – Zermingore
@Zermingore不要,特殊字符。 – ameyCU
是的,我假定ASCII是特殊字符。但我不確定它爲什麼會出現。 –