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!

One comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

w

Connecting to %s