Encrypting

Time for one more project! This checkpoint puts the strings and functions from the last two lessons to work, together with everything you learned before them. As in the earlier checkpoints, the instructions say what the program should print, and each exercise also runs your program with other data.

The project. You and a friend send each other secret messages. Over three exercises, your program will encrypt a message, decrypt your friend's reply, and finally crack a message whose key you don't know.

The cipher. You'll use one of the oldest ciphers, which Julius Caesar is said to have used: every letter moves a few places further along the alphabet. With a shift of 3, a becomes d, b becomes e, and so on. At the end of the alphabet, it goes on from the start: x becomes a, y becomes b and z becomes c. Uppercase letters stay uppercase, and everything that isn't a letter, such as spaces, digits and punctuation, stays as it is.

The data. The array message holds the message, and shift says how many places each letter moves. The program has to work for other messages too, and for any shift from 1 to 25.

The task. Print the message, and below it, the encrypted message:

Message: Meet me at the zoo by six
Secret: Phhw ph dw wkh crr eb vla

A tip: write a function that moves one letter and returns the new one. You'll need it again in the next two exercises. A character is a number (remember the lesson on variables), so you can calculate with it, and % brings you back to the start of the alphabet. The functions from ctype.h tell you whether a character is a letter and whether it's uppercase.

Instructions

Print the message and the encrypted message, as in the example.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/3