0

having an issue with caesar.c. I've got a loop working for lowercase cases where the key size would cause the key to loop out of alphanumeric characters, but when I try to implement the same loop for uppercase characters it does not work for some reason. The code goes as follows

if (plain [i] >= 66 || plain[i] <=90) //case for upper case letters

{

     if(plain[i]+k > 90) //case for "overflow" loop
     {

             if((plain[i] + (k%26) > 90)) //if modulo would send out of range

             {

                    plain[i] = plain[i] - 26 + k%26; //kick loop back 26 letters, then increment by remainder of key/26

             }

             else

             {

                    plain[i] = plain[i] + (k%26);//else increment by modulo key
             }
      }

      else
      {

             plain[i] = plain[i] + k; //else increment by key
      }
}

This works well for lowercase, where lowercase r, key of 65, goes to e as expected. However, with uppercase R, key of 65, it goes to _. This means that it is, for some reason, hitting the "increment by key mod 26" logical statement, instead of kicking the loop back by 26 letters and then incrementing by key mod 26. This is despite the fact that R plus 13 is greater than 90, which should be caught by the "if" statement. Any idea why that might be happening?

1 Answer 1

1

I've been trying to step through the code to duplicate what happens with no success, but I have a couple ideas. First, look at the following:

 if (plain [i] >= 66 || plain[i] <=90) //case for upper case letters

This statement is ALWAYS true because of the OR operator. It should have been the AND operator, &&. If the code uses || to test for lower case as well, it is likely that when you only had code for lower case, the test cases worked for letters, but if you didn't test for non lower case letters, no errors would show. Next, when the code for testing upper case was added, it got more complicated. If both the lowercase and uppercase tests used ||, then both blocks of code would process everything submitted.

Without seeing the rest of the code, I can't predict what's happening accurately, but my best guess is what I said above. Fix the test conditions and see what happens. If that doesn't fix it and you can't resolve, please edit the code and add the new updated code with the complete loop for processing the plain text, both upper and lower.

If this answers your question, please click on the check mark to accept. 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 .