Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 34 / 68

Building a Caesar cipher (a letter-shifting cipher)

This lesson covers implementing a Caesar cipher in Objective-C, with the aim of understanding how shifting letters encrypts text.

The Caesar cipher replaces each letter with another a fixed number of places along the alphabet. It comes from ancient Rome and is about as simple as a cipher gets.

The sample code assembles the result in an NSMutableString, taking each character with characterAtIndex: and shifting it while checking it is within the alphabet. Note the unichar type representing a character.

A common early stumble is the arithmetic on character codes. Cases where the shifted result runs past the end of the alphabet (A–Z) have to be handled correctly with % 26.

It is simple next to modern cryptography, but it makes a good introductory subject for practising arithmetic on character codes.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)