-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalphabeticShift.R
executable file
·35 lines (33 loc) · 987 Bytes
/
alphabeticShift.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Given a string, your task is to replace each of its characters by the next one in the English alphabet; i.e. replace a with b, replace b with c, etc (z would be replaced by a).
#
# Example
#
# For inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz".
# nput/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] string inputString
#
# A non-empty string consisting of lowercase English characters.
#
# Guaranteed constraints:
# 1 ≤ inputString.length ≤ 1000.
#
# [output] string
#
# The resulting string after replacing each of its characters.
# inputString = "crazy"
#new function learnt: intToUtf8
alphabeticShift <- function(inputString) {
inputString = strsplit(inputString,"")
inputString <- as.vector(unlist(inputString))
outputString <- lapply(inputString,function(x) {
if (x == "z") {
return(("a"))
} else {
return(intToUtf8(utf8ToInt(x) + 1))
}
})
return(paste(outputString,collapse = ""))
}