So, I am writing an encryption code. My code takes the word or any message and also asks user to enter a key. The end output is the encrypted message. For example:
Please enter the text you want to encrypt: hello
Enter the key: 4
The encrypted text is: lipps
But there is a problem. When I enter text that contains an s
inside, it gives a question mark for the encryption:
Please enter the text you want to encrypt: ssss
Enter the key: 13
The encrypted text is: ����
This problem doesn't occur when I write other keys than 13, and if the letter is uppercase. This problem happens when the text contains any letter that comes after s (t, v, u, w, x, y, z) and when the key is 13.
Aforementioned code is:
#include <stdio.h>
#include <string.h>
int main(void) {
int i;
int key;
char text[101], ch;
printf("Please enter the text you want to encrypt: ");
fgets(text, sizeof(text), stdin);
printf("Enter the key: ");
scanf("%i", &key);
for(i = 0; text[i] != '\0'; ++i){
ch = text[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
text[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
text[i] = ch;
}
}
printf("The encrypted text is: %s", text);
}