Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 34 / 68

Building a Caesar cipher (a letter-shifting cipher)

This lesson covers implementing a Caesar cipher in Julia, 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 walks the text with for c in text, checks that the character is within the alphabet, calculates the shifted code with (Int(c) - Int('A') + shift) % 26 + Int('A'), and converts back with Char(). Note how Int() and Char() convert between characters and numbers.

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 Julia 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)