# Pset2 - Caesar (less comfortable) wraparound problems

``````    if (isalpha(plain[i]))
if (isupper(plain[i]))
{
printf("%c", ((plain[i]-65) + k) % 26);
}
if (islower(plain[i]))
{
printf("%c", ((plain[i]-65) + k) % 26);
}
if (isblank(plain[i]))
{
printf(" ");
}
}
}
printf("\n");
``````

I hope someone can help clarify something for me. I included the section of code above where I was having problems. Namely, trying to wraparound from Z to A. I watched the walkthrough and understand that I need to convert from ASCII to alphabetical index. I believe I have a formula that would do that;

UPPERCASE plain[i] = ([i] - 65)

LOWERCASE plain[i]= ([i] - 97)

However when I compile the program it just outputs blank spaces for the cyphertext. Any help would be appreciated.

I got it working, maybe this will help somebody else. I'll detail my reasoning, so please correct me if anything is wrong.

In my code above I had written: (plain[i]-65) + k) % 26); as a way of converting from ASCII values to the alphabetical index. This seems to be correct. Using the letter M for example with an ASCII value of 77. So, 77 - 65 = 12. And the letter M does occupy the 12th position in the alphabet (if you are indexing from 0).

Next, adding the value of k successfully rotates the character by that many positions. For example, with a key of 5, M(which is 12) becomes R(which is 17).

In my understanding the modulo function essentially means that you cannot exceed a given value. In this case, the value is 26 since there are 26 characters in the alphabet. So upon reaching 26, we have wrap around back to 0.

For example, using the letter Y with a key of 2 would result in the letter A. Since Y(24) + 2 = 26. If you are indexing from 0 as I am above this means that Z occupies index no.25, aka the 26th letter in the alphabet.

When reading other questions and answers online there seems to be some variation as to whether people are indexing from 0 or 1 for the alphabetical index. Be aware of this.

Finally, to convert back from the alphabetical index to ASCII values simply add 65(for uppercase letters) after your equation.

For example:

if (isupper(plain[i])) { printf("%c", ((plain[i]-65) + k) % 26) + 65);

Using the previous letter M for example with a key of 5 this would mean (77-65) = 12) + 5 = 17) + 65) = 82 which is the ASCII value for uppercase R.

• "When reading other questions and answers online there seems to be some variation as to whether people are indexing from 0 or 1 for the alphabetical index." We always use zero indexing in this class. Care to point to the sources of your claim? Jul 14 '17 at 9:46
• cs50.stackexchange.com/questions/7336/… " Let's consider the following simplified formula. I've merely removed the subscript variables, because they're not needed for this explanation. c = (p + k) % 26 The English alphabet has 26 letters. Let's agree to represent those letters with numerical values 1 to 26, in the standard order. So we end up with a = 1, b = 2, c = 3, etc." But I thought A=0 through to Z=25? Jul 15 '17 at 21:24
• Hmmmm, you are right. In my opinion those are all bad answers, regardless the upvotes. The reputation of the answerer is often indicative of the quality of the question. Nevertheless, good question and thanks for the followup. Jul 15 '17 at 22:25

(plain[i]-65) + k) % 26

This will always be less than 26. So, it would never be an alphabet according to the ASCII table.