U13-Forward
U13-Forward

Reputation: 71570

Shift alphabetical characters in string

Let's say I have a string:

s <- 'hello world zzz'

I want to shift the alphabetical characters up by one.

So:

and so on...

And:


The other condition is that if there is character that isn't in the alphabet (in this case the space), keep the characters as it is, so the space remains as a space.


Would all this be possible?

My desired output here would be:

ifmmp xpsme aaa

I have tried:

new <- c()
for (i in s) 
    {
    new <- c(new, 'abcdefghijklmnopqrstuvwxyz'[which('abcdefghijklmnopqrstuvwxyz' == i) + 1])
    }

print(new)

But it doesn't work... It outputs nothing.

Any ways of doing this?

Upvotes: 0

Views: 150

Answers (1)

r2evans
r2evans

Reputation: 160417

chartr("abcdefghijklmnopqrstuvwxyz", "bcdefghijklmnopqrstuvwxyza", 'hello world zzz')
# [1] "ifmmp xpsme aaa"

(A function I've never had cause to use ...)

Upvotes: 3

Related Questions