I am making a Caesar's Cipher for my lab sheet, and have made it able to encrypt 3 subtitution(Caesar's Cipher), which is the point of the exercise. But there has been one thing bugging me. First, there is a trailing character if i put it other than 3. For example, by typing "malware", and 2 for key. This is my code :
#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 );
}
I hope I followed the correct indentation methods for coding. Got a lot of dislikes because of that