0

I got a lot to work in my code, except when I check it, it prints out the right encryption until I use capitals, yet I thought my capitals code was just fine. So if I use a key of abc, then it should print the phrase hi hat HOW, like hj jau JOX, but instead it prints hj jau PUD. Yet if I do all caps with no spaces, it prints out right. So it seems to be an error in the capitals with spaces, but I don't understand why. The key should enable no more than a 0,1,2 (repeating) shift... but that last part, the all caps, does an 8,6,7 shift. Which probably means the whole shift jumped from 0,1,2 to 6,7,8. But why? My isupper() method has the exact same calculations as my islower() method. Plus when I do all caps with no spaces, it works fine. :(

UPDATE: When I continue to run it, it looks like it has shifted entirely to 6,7,8 when all capitals are used, no matter whether there is a space or not. So somehow a 6 point shift happens permanently when I use all caps. It prints fine (the 0,1,2) when I use all lowercase, whether there are spaces or not.

The structure is like this:

for loop (that initializes i and j; i < plain text length; i iteration)
{
   if (plain text is lowercase)
     plain text[i] = plain text [i] - 'a' + (key[j % length of key] - 'a') modulus 26) + 'a';
     j iteration;
   if (uppercase)
     same code as above except 'A' in place of 'a'
}

printout: plaintext[i];
  • Good you found the solution; you could further improve your code by changing the second "if" condition for an "else if" condition, so that your code doesn't check all the conditions every time the for loop is running. – pat_krat Aug 13 '14 at 8:56
2

Wow, how nice is it to solve your own question, unexpectedly!!!

So the solution to my particular problem was in my ASCII math. Instead of - 'A' and - 'a' in my 2 if statements above for the key[j], they actually needed to be outside of those if statements. So I kept them in the for loop, but put 2 new ifs outside of the other 2 ifs. And amazingly the check50 was a glorious green. I love it when this happens!

I have no idea why I thought of the ASCII math, but it just kind of popped in my head. I think it worked because the ASCII math for each isupper and islower respectively was not working in an upper and lower if statement that was for the plain text array. It had to have its own if statements for the key array. Yay!

| improve this answer | |

You must log in to answer this question.

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