CMSC 246 Systems Programming
Spring 2018


Assignment#5
Due in Class on Wednesday, March 7, 2018

  1. Caesar Cipher
    One of the oldest known excryption techniques is the Caesar Cipher, attributed to Julius Caesar. It involves replacing each letter in a message with another letter that is a fixed number of positions later in the alphabet. For example, if each letter is replaced by one that is two positions after it, an A will be replaced by a C, an L by an N, etc. If the replacement goes past the letter Z, the cipher wraps around to the beginning of the alphabet. Thus, with the same shift (of two), a Y will be replaced by an A, and Z by B.

    Write a program that encrypts a message using the Caesar Cipher. The user will enter the message to be encrypted and the shift amount (number of positions by which letetrs should be shifted):

    Enter message to be encrypted: Go ahead, make my day.
    Enter shift amount (1-25): 3
    Ecrypted message: Jr dkhdg, pdnh pb gdb.


    You may assume that the message will not exceed 80 characters. Characters other than letters should be left unchanged. Lowercase letters remain in lowercase when encrypted, and uppercase letters remain uppercase.

    Hint: To handle the wrap around, use the expression ((ch-'A') + n) % 26 + 'A' to calculate the encryoted version of an uppercase letter, where ch stores the letter and n stores the shift amount. You will need a similar expression for lowercase letters.

    Task 1: Write, then use, a function called encrypt1() whose header is shown below:

    void encrypt1(char message[], char codedMsg[], int n, int shift);
    // Encrypts the text in message[0..n-1] using Caesar Cipher with shift (1 <= shift <= 26). Result is left in codedMsg[]

    Task 2: Write, then use, a function called encrypt2() whose header is shown below:

    void encrypt2(char *message, char *codedMsg, int shift);
    // Encrypts the string in message[] using Caesar Cipher with shift (1 <= shift <= 26). Result is left in codedMsg.


    This fucntion is the same as encrypt1() except, this time you will write it using pointers and pointer arithmetic, on a string (which is NULL terminated).


Scripting in Linux
Once done, you will need to show the sample runs of programs, as required below. In order to record the runs, you can use the Linux command script.
Go to your Assignment directory and enter the command: script session

You will get your command prompt back. Next, run each of the above programs as you normally would. Once done, enter the command: exit (or CTRL-D)

This will end your script session. If you now look a the contents of the session file, you will see that the entire interaction has been recorded in it (as a script!).

What to hand in:

Back to CMSC246 Home page