Created
February 1, 2021 01:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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