on pset2/caesar and only when ciphertext[i] = A or a for loop stopped ????enter image description here

2 Answers 2


In addition to Kley's answer, "A" or 'a' will break it because of a subtlety that you are probably unaware of.

Consider the interaction of the for loop, the code processed inside the loop and how strlen() works.

for(i = 0; i < strlen(plaintext); i++)
    [code that replaces plaintext chars with encoded values - 65 or 96.]

First, understand how strlen() works. Each time it is called, it searches a string for the end of string marker, '\0', or 0x00. It then returns the number of characters checked before 0x00 is found.

Next, note that the for loop will call strlen(plaintext) as part of the test for every pass.

Finally, look at what happens inside the loop. Each character is partially encoded. Letters are converted to values between 0 and 25 inclusive. Specifically, "A" or "a" are replaced in the string with 0, or 0x00. The code doesn't add back 65 or 96 to transform it back to a letter. It adds 65 or 96 to the last stored value only to print it.

Now, let's put all this together. An "a" is processed. It leaves 0, or 0x00 in the string in place of the "a". When the next loop pass is started, the check calls strlen(plaintext), which finds 0x00 where the "a" used to be and decides that it's the end of the string. In short, the test fails, the loop terminates, and the rest of the plaintext remains unprocessed.

There are a couple ways to fix the code. One would be to add 65 or 96 back to the char and store in plaintext before printing it.

But the better, more efficient fix is this. ALWAYS, calculate the string length and save it in a var before the loop!

my_str_len = strlen(plaintext);
for(int i = 0; i < my_str_len; i++)

This has two distinct advantages. First, the string length is calculated only once, instead of every pass of the loop. That saves computer cycles. (imagine a production program where the loop executes 10 million times. That's a lot of saved processing time!)

Second, the calculated length cannot change (as long as the code doesn't alter my_str_len). That means that you don't have to worry about an errant 0x00 showing up in the string!

Happy Coding!


Think about how for loops works. They will only iterate over what's inside it:

for (int i = 0;...)
    // Your code here

In C, those curly braces are mandatory.

FYI: The best practice when asking a question is to copy/paste part of your code, rather than adding a screenshot. It will help us run and debug your code.

  • yes , i do that and it work good with any output except "A" .... Commented Sep 25, 2021 at 20:31
  • @Kley Halisson, technically, this isn't correct. It's always best practice to use curly braces, but not mandatory. They are only required for multiple lines of code to be processed in the loop. Without the curly braces, only the following line will be executed as the object of the loop, up to the next semicolon, with certain "exceptions". (continues....)
    – Cliff B
    Commented Sep 26, 2021 at 4:29
  • (... continued) This code demonstrates the exception. While it looks like multiple lines of code, it's really a single line!!! It's a for/if construct, or more accurately, for/if/elseif/else. That means that everything from the end of the for statement to the closing curly brace of the else statement are constructively a single line of code!
    – Cliff B
    Commented Sep 26, 2021 at 4:29

You must log in to answer this question.

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