My Caesar for problem set 2 seems to encrypt my plaintext correctly. My problem is just that it won't print any non-alphabetic characters.
For instance, when my plaintext is "hello, world" and the key is 13, my ciphertext is "uryybjbeyq" when it should be "uryyb, jbeyq".
At first, I thought my code was encrypting the non-alphabetical characters like it would for normal letters but it looks like my code is just skipping over it instead. Here is a sample of my code:
int key = atoi(argv[1]);
printf("plaintext: ");
string message = get_string();
// Here I am looping through the plaintext. I am checking if each letter is in the alphabet. If not, I just print it out.
// If the letter is in the alphabet, I check to see if it is upper or lower case and encrypt it.
printf("ciphertext: ");
for (int i = 0; i < strlen(message); i++){
if (isalpha(message[i])){
if (isupper(message[i])) {
char letter = message[i] - 65;
char encrypt = ((letter + key) % 26) + 65;
printf("%c", encrypt);
}
else if (islower(message[i])){
char letters = message[i] - 98;
char encrypted = ((letters + key) % 26) + 98;
printf("%c", encrypted);
}
else {
printf("%c", message[i]);
}
}
}
printf("\n");
return 0;
I've been stuck on this for a while now and I can't find anything that can solve the problem! I would appreciate any help :)