When I run my code, anytime I try to use the modulo function, I get a strange symbol for an answer. I can't post it because it wouldn't download, but it is a square with 4 numbers inside. Why am I getting this symbol? I know it is caused by the modulo function because it only occurs when I use modulo. Am I using modulo wrong?

        if ((isalpha(plaintext[i])) && (isupper(plaintext[i])))
            int ciphertext = ((plaintext[i] + key) % 26);

                printf ("%c", ciphertext);    

1 Answer 1


Modulo is working correctly, but you've overlooked something. The idea behind using modulo is this. When you represent letters as numbers between 0 and 25 inclusive, and add a number to shift the letter, it may go beyond 25. You can apply % 26 to wrap back to the 0 to 25 range. But it ONLY works if the letters are represented by numbers in that range.

Think about this. What if you apply % 26 to the numeric value of any letter when it is represented by 0 to 25? Nothing happens. Unfortunately, letters are represented in the computer by ASCII codes. Consider this: 'a' % 26 = 19, an unprintable control character.

BEFORE applying any modulo operations to the ASCII value of a letter, you have to convert it to the 0 to 25 range first. Once you're done, you have to convert back to ASCII values by reversing the operation. If you don't understand this concept, you should go back and review the class material.

If this answers your question, please accept the answer to remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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