Created
August 8, 2022 15:42
-
-
Save Husseinhj/666b446860c0443228fe1ce7e1c8f2aa to your computer and use it in GitHub Desktop.
Replace every second character of a word in a text with a specific symbol
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
package org.kotlinlang.play | |
fun main() { | |
val sample = "Replace every second character of a word in a text with a specific symbol" | |
val words = sample.split(" ") | |
println(replaceCharInWords(words).joinToString(" ")) | |
} | |
fun replaceCharInWords(words: List<String>, index: Int = 1, with: Char = '@'): List<String> { | |
return words.map { | |
if (it.length > index) { | |
var chars = it.toCharArray() | |
chars[index] = with | |
return@map chars.concatToString() | |
} | |
return@map it | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment