0

I have one problem. So I have to create a program that encrypts the inputted text by replacing every letter with the positioning number of this letter position in the alphabet (example: text=ABC, coded message = 123). How can I do this?

  • Are you talking about TEXT ie whole sentences with spaces, commas, punctuations, LF, empty lines, or (just) a single word? – GWD Nov 25 '20 at 15:42
  • There are dedicated packages for this task, also, read about `charToRaw`, and `rawToChar`. – zx8754 Nov 25 '20 at 16:29

3 Answers3

2

This works

library(tidyverse)

input_string <- "The quick brown fox jumps over the lazy dog"

for(i in 1:26) {
  
  input_string <- str_replace_all(input_string, 
                                  regex(letters[i], ignore_case = TRUE), 
                                  as.character(i)
                                  )
  
}

input_string
[1] "2085 17219311 218152314 61524 1021131619 1522518 2085 1212625 4157"

Or if you prefer to use mgsub()

devtools::install_github("bmewing/mgsub")
library(mgsub)

input_string <- "the quick brown fox jumps over the lazy dog"

for(i in 1:26) {
  
  input_string <- mgsub(input_string, 
                        regex(letters[i], ignore_case = TRUE), 
                        as.character(i)
                        )
  
}

input_string
[1] "2085 17219311 218152314 61524 1021131619 1522518 2085 1212625 4157"
stevec
  • 41,291
  • 27
  • 223
  • 311
  • How would you decypher this message? How would you know if '11' meant 'AA' or 'K' ? – Chris Nov 25 '20 at 15:09
  • @Chris you wouldn't know. Replacement like the OP says (`ABC` with `123`) would not allow for 'decryption' (term used loosely). Could always use a separator though (e.g. a hyphen or comma between each substitution), then 'decryption' would be possible. – stevec Nov 25 '20 at 15:11
  • 1
    Of course you are right, that the question was to only replace letters with numbers. I like your approach. I was just curious if you maybe also thought about this option. – Chris Nov 25 '20 at 15:24
1
encrypt <- function(string) {
  spstring <- unlist(stringr::str_split(tolower(string), ""))
  unlist(lapply(spstring, function(x) which(x == letters)))
}

encrypt("Test")
#> [1] 20  5 19 20

Created on 2020-11-25 by the reprex package (v0.3.0)

Florian
  • 1,248
  • 7
  • 21
0

Good old base-R sapply based solution in case you just want to "encrypt" words and there are no special characters involved.

> a <- "TesT"
> sapply(unlist(strsplit(x = toupper(a), "\\b")), match, LETTERS)

 T  E  S  T 
20  5 19 20

Transform the word in vector a to upper case, run the string split on single letters \\b (regex) unlist the result and sapply the match function against R's (internal UPPERCASE) LETTERS vector. You could also turn the character string tolower and match it against letters. Of course there is no easy way for you to "decrypt" that without having some information about the original text eg 24 could mean the letter "x" or "bd".

GWD
  • 1,387
  • 10
  • 22