Skip to content

Instantly share code, notes, and snippets.

@Husseinhj
Created August 8, 2022 15:42
Show Gist options
  • Save Husseinhj/666b446860c0443228fe1ce7e1c8f2aa to your computer and use it in GitHub Desktop.
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
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