Compute generalized ‘rot13’ character translations or “rotations”

In the distant past, considered as poor man's encryption, such rotations are way too poor nowadays and provided mainly for didactical reasons.

rotn(ch, n = 13)

Arguments

ch

a character vector; often a string (of length 1).

n

an integer in \(\{1\dots26\}\); the default is particularly useful.

Value

a character as ch, but with each character (which belongs to letters or LETTERS “rotated” by n (positions in the alphabet).

Author

Martin Maechler

Details

Note that the default n = 13 makes rotn into a function that is its own inverse.

Written after having searched for it and found seqinr::rot13() which was generalized and rendered more transparently to my eyes.

See also

rot2, a completely different rotation (namely in the plane aka \(R^2\)).

Examples

rotn(c("ABC", "a","b","c"), 1)
#> [1] "BCD" "b"   "c"   "d"  
rotn(c("ABC", "a","b","c"), 2)
#> [1] "CDE" "c"   "d"   "e"  
rotn(c("ABC", "a","b","c"), 26) # rotation by 26 does not change much
#> [1] "ABC" "a"   "b"   "c"  

(ch <- paste("Hello", c("World!", "you too")))
#> [1] "Hello World!"  "Hello you too"
rotn(ch)
#> [1] "Uryyb Jbeyq!"  "Uryyb lbh gbb"
rotn( rotn(ch ) ) # rotn(*, 13) is its own inverse
#> [1] "Hello World!"  "Hello you too"