Skip to contents

This can be used to create (encrypt) and solve (decrypt) a Caesar cipher. The function does not differentiate between the two.

The Caesar Cipher Wikipedia entry provides more information on the methods used: https://en.wikipedia.org/wiki/Caesar_cipher

Usage

caesar(x, n = 1, preserve_spaces = TRUE, dict = NULL, preset = NULL)

Arguments

x

A vector to be shifted

n

(Default: 1) The number of places to shift by. This can be either positive or negative. Zero returns x as it was given to the function.

preserve_spaces

(Default: TRUE) A boolean describing if spaces should be preserved. This is helpful when working with sentences.

dict

The dictionary used for shifting. This defaults to NULL in which case a dictionary is built from the sorted unique values of x.

preset

A pre-made dictionary using ASCII codes from https://www.ascii-code.com/. Note that delete is excluded as a character.

  • NULL (the default)

  • "alphanumeric": ASCII characters 48:57, 65:90, and 97:122. Numbers 0-9 and both uppercase and lowercase letters from the English alphabet.

  • "keyboard": ASCII characters 32:126. The characters you'll find on a US English keyboard.

  • "letters": ASCII characters 65:90 and 97:122. Both uppercase and lowercase letters from the English alphabet.

  • "lowercase": ASCII characters 97:122. Lowercase letters from the English alphabet.

  • "uppercase": ASCII characters 65:90. Uppercase letters from the English alphabet.

Value

A character vector of length one that has been shifted.

Examples

(e1 <- caesar("abcde", 1))
#> [1] "bcdea"
caesar(e1, -1)
#> [1] "abcde"

(e2 <- caesar("cipheR is a great R package!", -5))
#> [1] "saeRtp ah r !gtri p erscr!tk"
caesar(e2, 5)
#> [1] "cipheR is a great R package!"

(e3 <- caesar("Isn't this fun?", 2, preserve_spaces = FALSE))
#> [1] "hutI ? nsu?i'tf"
caesar(e3, -2, preserve_spaces = FALSE)
#> [1] "Isn't this fun?"