Skip to content

Instantly share code, notes, and snippets.

@mateohervas
Created February 1, 2021 01:13
Show Gist options
  • Select an option

  • Save mateohervas/e9f29401fab5cbee54d83092985c20d8 to your computer and use it in GitHub Desktop.

Select an option

Save mateohervas/e9f29401fab5cbee54d83092985c20d8 to your computer and use it in GitHub Desktop.
Given a string str consisting of letters only and an integer n, the task is to replace every character of the given string by a character which is n times more than it. If the letter exceeds ‘z’, then start checking from ‘a’ in a cyclic manner.
fun main() {
replaceByZ("abc", 28)
}
fun replaceByZ(string: String, z: Int) {
var newString = ""
string.forEach { character ->
var aux = z
newString += if (character.toInt() + z > 122) {
aux -= (122 - character.toInt())
aux %= 26
(aux+96).toChar().toString()
} else {
(character.toInt() + aux).toChar().toString()
}
}
print(newString)
}
//To answer question number 2: What is the disadvantage of using the ASCII value of the letters to solve this problem?
//Because ASCII has a limit of characters it can hold, that limit will also limit the amount of alphabets that it can store and thus it won't be useful for other alphabets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment