Embed ROT-13 in R

I need a function that, when transmitting a string containing only letters, rotates each letter in the string through the alphabet with the characters X, where X is the parameter of the function. A famous example of this is X = 13, which is called ROT-13.

function <- ROTx (str, x) { ?? }

This is what I expected master R to do just a few lines, while I would have 10 or more.

+4
source share
2 answers
rotX <- function(ch,x) { #rotate each letter of a string ch by x letters thru the alphabet, as long as x<=13 old <- paste(letters, LETTERS, collapse="", sep="") new <- paste(substr(old, 2*x+1, 26*2), substr(old, 1, 26), sep="") chartr(old, new, ch) } 

This fixes both issues that I noticed in my comment.

+3
source

See ?chartr (section "Examples"):

 rot <- function(ch, k = 13) { p0 <- function(...) paste(c(...), collapse="") A <- c(letters, LETTERS, " '") I <- seq_len(k) chartr(p0(A), p0(c(A[-I], A[I])), ch) } 

or here http://rosettacode.org/wiki/Rot-13#R :

 rot13 <- function(x) { old <- paste(letters, LETTERS, collapse="", sep="") new <- paste(substr(old, 27, 52), substr(old, 1, 26), sep="") chartr(old, new, x) } 
+6
source

Source: https://habr.com/ru/post/1336289/


All Articles