Cracking the Code

You've intercepted a message that someone else sent. It's in the new array intercepted, encrypted with the same cipher, but you don't know the shift. You can work it out, though: in English, the most common letter is e. In a short message, it doesn't have to be so, but in this one, it is. The message is all in lowercase letters.

Find the most common letter of the message. The shift is how many places that letter comes after e, because that's what e turned into. Then decrypt the message with the shift you found. The output ends like this:

Reply: Ok, I will bring the map
Most common letter: l
Shift: 7
Decrypted: the treasure is buried under the old tree

When the most common letter comes before e in the alphabet, the shift went past z and on from the start: if e became c, the shift is 24.

A tip: remember the function count_letter from the lesson on functions, which counts how many times a letter appears in a text. Try it with every letter from a to z, and keep the one with the highest count, the way you found the largest value in the lesson on arrays.

That's the whole project. Your program encrypts, decrypts and even cracks secret messages, with functions of your own that work with strings. In the next lesson, on pointers, you'll find out what addresses in memory are and how a function can change a variable of the code that called it.

Instructions

Print the most common letter and the shift.

Print the decrypted message.

Start programming for free

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

Or sign up with:

3/3