Home > Net >  In my R implementation of a Caeser Cipher, spaces don't convert
In my R implementation of a Caeser Cipher, spaces don't convert

Time:06-08

I'm writing Caesar's code. The code works, but the space doesn't apply. For example, "AB" can be transformed, but "A B" is not converted.

Caesar = function(input){

answer = ""

solution = NULL

Low = "abcdefghijklmnopqrstuvwxyz"

Up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Lower = strsplit(Low,"")

Lower = Lower[[1]]

Upper = strsplit(Up, "")

Upper = Upper[[1]]

cutI = strsplit(input,"")

cutI = unlist(cutI)

df_Result = data.frame(n.Kan = 1:26)



for(n in 1:26){

answer = ""

for (i in cutI){

if (i %in% ""){

answer = paste0(collapse = "")

} else if (i %in% Lower){

ind = which(Lower == i)

indN = (as.numeric(ind) n)%&

if(indN %% 26==0){

indN = 26

}

answer = paste0(c(answer, Lower[indN]),collapse="")

} else{

ind = which(Upper == i)

indN = (as.numeric(ind) n)%&

if(indN%&==0){

indN = 26

}

answer = paste0(c(answer, Upper[indN]), collapse = "")

}

answer=paste0(answer, collapse="")

}

solution = c(solution,answer)

}

df_Result = cbind(df_Result , "Result" = solution)

return(df_Result)

}

CodePudding user response:

I think there are other ways to improve this function you have. But the quickest solution could be using str_remove_all() from the stringr package.

So at the very top of your function you could include the line:

input <- stringr::str_remove_all(input, ' ')

CodePudding user response:

Rather than

if (i %in% ""){

answer = paste0(collapse = "")

}

you probably meant

if (i %in% " "){

answer = paste0(c(answer, i), collapse = "")

}

or simpler

    if (i == " ")
        answer = paste0(answer, i)
  • Related