0

I have only one error in check50:

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "CaQGoh\n"

I have no idea why, here's my code:

int lc = 0;
        for (int i = 0, j = strlen(ec); i < j; i++) {
            int count = lc % strlen(k);
            //Encrypt lowercase
            if (islower(ec[i]) && isalpha(ec[i])) {
                ec[i] = ((ec[i] - 'a' + k[count] - 97) % 26) + 97;
                lc++;
            }

            //Encrypt uppercase
            if (isupper(ec[i]) && isalpha(ec[i])) {
                ec[i] = ((ec[i] - 'A' + k[count] - 65) % 26) + 65;
                lc++;
            }
        }
1

Your code checks for upper vs. lower case of the string to be encrypted, but not of the key.

If you look at the test string and the key in this case, notice that there is only one instance where there is a case mismatch between the key and the string to be encoded, specifically, the last 'o'. It encodes to a letter that is off by 6 letters.

Look at the ASCII codes for caps vs. lower case.
'A' = 65 'a' = 97.
'a' - 'A' - 26 = 97 - 65 - 26 = 6.

There's an offset of 6 between upper and lower case alphabets. So, you see that you have to consider the case of the letter.

Or, what if you first convert the key from mixed case letters to simply the actual numbers that the shift needs to be. For instance, 'A' or 'a' would be a shift of 0. I'll leave it to you to figure out how.

5
  • Didn't realize that both A and a are shifts of 0, thanks!
    – Nikhil Jha
    Apr 11 '15 at 17:01
  • Wait, no, still encrypting wrong. I made all the chars in the string tolower() and I'm getting even more errors.
    – Nikhil Jha
    Apr 11 '15 at 17:15
  • I assume that you meant that you made the KEY string all lower case. Did you leave them encoded in ASCII (a=97) or (hint, hint) did you do something else with them after that?
    – Cliff B
    Apr 11 '15 at 18:15
  • Thought about it some more. You say it has gotten worse. Is it possible that it is correctly translating all of the lower case letters, but not the upper case ones? If so, think about what isn't working and how to fix it. If not, and if you can't figure it out, can you post that block of code and the input and output from a run?
    – Cliff B
    Apr 11 '15 at 18:59
  • Hi Cliff B and Nikhil, I am currently experiencing the same issue with my code and can't seem to find much help on the questions already posted. My output is the same exact output as the one above. What was the solution to this error? thank you so much!
    – jillbas
    Jul 31 '15 at 21:17

You must log in to answer this question.

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