Lab 9 – Caesar Cipher in C
20 points
Due Friday May 26th, by


Write the Cesar Cipher in C, just like the one we implemented in LC-3 assembly in lab8. Make sure you read through to understand the implementation-specific specifications.


You want to be able to hang out with the cool kids that use the Caesar Cipher ( to tell jokes on the internet. So you decide to write a program to help you convert jokes into and out of any Caesar Cipher format (ROT-13 is an example of one).  Your assignment is:


a) Create space for two strings (array of characters) as global variables.
b) Print a prompt that asks to either encrypt or decrypt a string, or ‘x’ to quit.

c) Print a prompt that ask the user for a number between 1 and 25, the shift amount.

d) Print a prompt that asks for the string to encrypt or decrypt.

e) Either encrypt or decrypt the string, printing out the number of encrypted/decrypted characters.

f) Print out both the source string and the encrypted/decrypted string.
g) Repeat from b)

That is it.

The Caesar Cipher Algorithm

The Caesar Cipher is an encoding method that works like this (from wikipedia) .

Encryption of a letter x by a shift n can be described mathematically as,


E_n(x) = (x + n) \mod {26}.

Decryption is performed similarly,

D_n(x) = (x - n) \mod {26}.





Your code should consist of three functions: main(), encrypt(), and decrypt().



unix1 33> gcc –o lab9 lab9.c


will compile your cesar.c program, and generate an executable file called cesar. You can then run your program by typing


unix1 34> lab9









unix1 55> man printf


            and to learn more about man itself, you can man man:


unix1 56> man man




You are not allowed to discuss this lab with other students.

You are allowed to work in a pair on this assignment.

Figure it out on your own and ask for help from the TA/tutor if needed.


Here’s how mine looks:


unix1 82> gcc -o lab9 cmpe12s06lab9.c

cmpe12s06lab9.c: In function `main':

cmpe12s06lab9.c:19: warning: return type of `main' is not `int'

unix1 83> lab9

Welcome to the encryption/decryption program!


Encrypt (e), decrypt (d), or exit (x) ? e

Shift amount = 7


String to encrypt: This is a lot easier in C than in LC3 assembly!

Characters encrypted: 35


String in clear :  This is a lot easier in C than in LC3 assembly!

String encrypted:  Aopz pz h sva lhzply pu J aohu pu SJ3 hzzltisf!


Encrypt (e), decrypt (d), or exit (x) ? x


Program end - bye!

unix1 84>


Files to Submit:


You must demonstrate this lab to the TA/tutor, either before submission or after submission.   To be considered on time you must have submitted everything by the due date.



Check out the grading template to see the break down of how things will be graded.