Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 34 / 68

Building a Caesar cipher (a letter-shifting cipher)

This lesson covers implementing a Caesar cipher in R, 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 converts a character to its code with utf8ToInt(), calculates the shifted code while checking it is within the alphabet, and converts back with intToUtf8(). Note strsplit(text, "")[[1]], which splits the string into a vector of single characters.

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 manipulating character codes is the basis of more advanced encryption and encoding work.

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

๐Ÿงช This site can't compile or run R 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)