I'm finishing up the Ceasar Cipher and I have it working but for the non-letter characters which are also affected by my program.

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main (int argc, char* argv[])
int k;

    printf("Please give me the number of positions forward to shift, please. ");
    k = GetInt();

while (k <= 0);

printf("Please give me the phrase that will be altered in this program. \n");
string s = GetString();

for (int i = 0, n = strlen(s); i < n; i++)
printf("%c", s[i]+k %26);

return 0;

My problem is that any characters that aren't the letters are being effected as well. I know that this project is missing some of the includes from the hw, and I have another version with things like argc == 2, etc. but I'm wondering if this can be fixed without those includes.

I'd also like to get the checker prompts, my checker has yet to work and I'd like to see if my program works for what they are checking for.


  • Hi Mate, you need to use isalpha to check for letters... anything else then your program will need to skip over. Sep 1, 2015 at 8:52

2 Answers 2


You have several issues to fix. First, as Dulcie indicated, you need to encode ONLY the alphabetic characters. The isalpha() function will be very useful to do this. If something isn't alpha, then it should simply be reprinted.

Next, look at this line:

printf("%c", s[i]+k %26);

s[i] will be an ASCII representation of a letter, to which the shift value is added. Unfortunately, the ASCII values are at least 63. If you apply the modulo operation to it, the result will not be what you want. Try working it out on paper and you'll see what happens. The modulo reset only works when the letter is between 0 and 25 inclusive. You need to convert from ASCII to a 0-25 number, shift, perform the modulo, and then convert back to ASCII in the same case.

This should get you a lot farther down the road.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

Part of programming is to precisely understand a programming assignment or job. When a job has specific requirements, getting fancy or creative with how you do it may seem like a good idea, but if it deviates from the specification it is bad. You're about to learn that check50 will compile your code and run it with test data and will expect very exact output. Extra or missing characters, line feeds, prompts, or anything else will result in a test failure under check50, basically because you didn't do exactly what was required.

For instance, prompting for the shift key instead of just taking it as a parameter is a fail. So is giving a prompt for the text to be encoded.


Just use isAlpha(), and if true, print the encoded character. Otherwise, just print the character as is.

You must log in to answer this question.

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