I'm stuck at encrypting uppercase text. My code works fine for lowercase text, but will return wrong output when it comes to encrypting uppercase chars.

for (int c = 0; c < length_of_ptext; c++) {

        if ((isupper(ptext[c])) && (islower(shift[c])))
            ctext[c] = (((ptext[c] - 'A') + (shift[c] - 'A')) % 26) + 'A';//encryption of uppercase characters with lowercase key

        else if ((islower(ptext[c])) && (islower(shift[c])))
            ctext[c] = (((ptext[c] - 'a') + (shift[c] - 'a')) % 26) + 'a';//encryption of lowercase characters with lowercase key

            ctext[c] = shift[c];//non-alphabetical characters remain as they are

1 Answer 1


Are you sure that's your problem? Looking at your code, it appears that it only handles the two cases where the key and the plaintext are both the same case. It doesn't handle the two cases where the key and the plain text are different cases, one upper case and the other lower case.

It also appears that you are using the same index, c, for both the plaintext and the key. This will blow up on you when a non-alpha is processed, or when the length of the key is exceeded.

Without seeing the actual input and output, or more code, there's no way to accurately determine the problem, but one or both of these problems are likely to be the cause.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • It looks like the only problem was that I hadn't worked out, that I was only considering text and key with same case. As for the problem with the same index c, I'm handling it in another piece of my code. Thanks for the quick response. Apr 2, 2016 at 7:13

You must log in to answer this question.

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