0

Hi everyone and I'm sorry if this has been asked here, but I've spent hours online trying to figure out why only the mixed (upper and lower) tests failed. I don't know what I'm overlooking or missing out.

Here is a sample of my calculation.

 for (int i = 0, n = strlen(p); i < n; i++)
   {

       if isupper(p[i])
       {
           printf("%c", ((p[i] -'A' + k) % 26) + 'A');
       }

       if islower(p[i])
       {
           printf("%c", ((p[i] - 'a' + k) %26) + 'a');
       }

       else
       {
           printf("%c", p[i]);
       }
   }

    printf("\n");
    return 0;
}

Here are the results of checkcs50: BARFOO with 3 keys:- expected output: EDUIRR actual output: EBDAURIFRORO

BaRFoo with 4 keys:- expected output: FeVJss actual output: FBeVRJFss

I really appreciate any help! Thank you all in advance.

2

Go through your conditionals if p[i] is a capital letter. if islower(p[i]) is a plain "if" statement, so you'll go into the "else" block if the character isn't a lower case letter. It looks like you want that to be an "else if" block, instead.

Debugging tip: since you have the expected output and the actual output, look and see if you can find patterns in what's wrong when you're trying to find what's wrong with your code. The thing that lead me to seeing what was off was noticing that your actual output was longer than it should be, then noticing that every second character in "EBDAURIFRORO" is the original string.

1
  • I really really really can't thank you enough! It's like you opened my third eye wow haha but seriously you have officially made my day :) I was too focused on one thing and turned out to be the simplest. Thanks again!
    – elizaponsm
    Jun 10 '20 at 22:17

You must log in to answer this question.

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