1

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 :)

1
  • i think that you have to use debug50 first and try to track the error then include specific code not the entire file Oct 18, 2017 at 21:37

2 Answers 2

1

You have a misplaced semicolon! The one you've opened after 'isalpha' condition you should close after the first else not after the second one!

1
  • This solved the whole problem! Thanks so much :D
    – Tenacity
    Oct 19, 2017 at 14:23
1

in principle your encryption formula seems correct, except for a "small" detail

 char letters = message[i] - 98;
 char encrypted = ((letters + key) % 26) + 98;

I would like to know why precisely 98 appears here, should be 97, think about it carefully

1
  • Thank you. I just Googled the Ascii chart and I saw that 'a' actually starts at 97 instead of 98. I have now fixed that part of my code :)
    – Tenacity
    Oct 19, 2017 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .