In this exercise, we were tasked to create a cipher that would encode some text from the user via the command line. So, you will notice that our main function now has some new parameters other than the “void” that has been used in the previous exercises. This is how we tell our program how many command line arguments to expect. Notice, that I have included several libraries for this particular exercise. These are needed to access the functions that we will be using to make Caesar’s cipher.

To start, our program always has one command line argument, which is the programs name. So, to make sure that we get one and only one command line argument from the user we will make use of the first if statement. This if statement is saying that if there are not two command line arguments, which would be the program name and some user input, then prompt the user to “Try Again.” Returning 1 tells the program that it encountered an error. This number could be anything. I chose to use 1 for simplicity.

While int argc tells us how many command line arguments there are, argv will give us the actual string that the user typed in. In this cipher the user is putting in a number that will tell us how many places to shift a letter in the alphabet. So, we need to convert that string into an integer. This is where we use the atoi function that is called from the stdlib.h library. Now, we also need to make sure that the integer we receive is non-negative. So, the next if statement will, again, prompt the user to “Try Again” if the number that they entered is negative.

In the else portion of the if statement is where we will actually get some text input from the user and encrypt that text using the number that the user entered into the command line.  So, we prompt the user for some input and then also create a placeholder for where the cipher text will go once the encryption is complete. Now we need to encrypt the message, which we will achieve using a for loop.

The for loop is going to execute from 0 to the length of the plain text that the user typed in to have encrypted. Notice that we have an if, else if, else statement that will manage the input to cipher. Remember that we need to preserve any capitalization and preserve any other characters that the user will input besides A-Z. This is where we make use of the isupper function and the islower function contained within the ctype.h library. Now the printf functions bellow the isupper and islower may look a little cryptic, but, once they are explained it will make sense.

First, we are taking the character and adding to it the number of spaces that we need to move that character. Then we subtract 97 from that number which will give us the number of the letter in our 26 character alphabet. Remember, that in our ASCII chart our lower case numbers start at 97, and our upper case start at 65. So, we need to adjust for that to bring them into our 26 letter alphabet. Next, we use some modular arithmetic to make sure that our cipher has the ability to wrap around. Thus, if the user types in 27, we would wrap all the way back around and shift the letter by only one space. Finally, we add 97 (or 65 for upper case) back to the character to give us the proper ASCII representation.  At the end we return 0 to the program to signify that everything ran correctly.

This was Caesar’s cipher. If you have any questions about my solution, please drop them in the comments section below. Remember to share this post on your favorite social media platform by clicking one of the buttons below. For more programming tips follow me on Twitter or GitHub and as always……Happy Programming!


  1. Im so glad I found your blog I got mega stuck on this one!!!

    Can you explain the (((plainText[i] + k) – 97) % 26) + 97) bit again? I really don’t get it I know you explain it in your blog but the modulo bit is really hard for me to understand Im googling but I don’t get why we need a remainder in this equation??? 😦

    It’s also interesting seeing our slight differences in code! I love it everyone finds their own way!



    1. I’m glad that you are finding it helpful. I also enjoy seeing others solutions to problems. It shows that we all think a little differently when developing the solution. So, let’s talk about the modulo arithmetic. In the problem set a requirement is that a person could enter a number larger than 26 (or the number of letters in the alphabet). So, if they enter 27 we want to be able to loop back around. If you look at the ASCII chart you will see that the lower case letters start at 97. If we do not use modulo arithmetic, if a user entered 27 ASCII character returned would be { instead of a. So, using modulo arithmetic of 27 % 26 will give us the remainder of one which allows us to avoid printing out characters that are not letters. Here is a good video on the subject of modulo arithmetic: Although this example is in Python, the concepts are all the same. Hope this helped. Good luck.


  2. I can see the same problem, I am facing now.
    If I will put it like this
    $ ./caesar 20x (or simply any character instead of a number)
    Your program will continue to work instead of giving “Usage: ./caesar key”.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s