BBK#3: Mumbling...
Don't be a Java-ish dev in Kotlin-ish world, improve your knowledge about Koltin Standard Library and write better Kotlin code. ✌🏻 If your Java dev migrating to Kotlin this will help you learn alot!
Question
: This time no story, no theory. The examples below show you how to do
mummble("abcd") -> "A-Bb-Ccc-Dddd"
mummble("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
mummble("cwAt") -> "C-Ww-Aaa-Tttt"
Try it put yourself :
👨🏻💻👉 https://www.codewars.com/kata/5667e8f4e3f572a8f2000039/train/kotlin
This solution requires accumulator pattern, read through my article on this it will help you out a lot!

Solution 1:
fun mummble(input: String): String {
val words = input.toCharArray()
val mumble = StringBuilder()
for ((index,word) in words.withIndex()) {
mumble.append(word.toUpperCase())
repeat(index){
mumble.append(word.toLowerCase())
}
if(index != words.size -1){
mumble.append("-")
}
}
return mumble.toString()
}
// Performance : took ~ 1.187 ms on my machine
OR
fun mummble(input: String): String {
var result = ""
input.toCharArray().forEachIndexed{ index,item->
result = "$result${item.toUpperCase()}"
repeat(index){
result = "$result${item.toLowerCase()}"
}
if(input.length-1 != index){
result = "$result-"
}
}
return result
}
// Performance : took ~ 0.631 ms on my machine
forEach
under the hood is same as for
, it's giving more performance because I have used StringBuilder
in one and other uses String Object
Solution 2:
Using fold
, checkout :

fun mummble(input: String): String {
val answer = input.toCharArray().foldIndexed(""){ index,acc,item->
var result = "$acc${item.toUpperCase()}"
repeat(index){
result = "$result${item.toLowerCase()}"
}
if((input.length - 1) != index){
result = "$result-"
}
result
}
return answer
}
// Performance : took ~ 0.180 ms on my machine
Enjoying the Post?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
If you want to support my content do consider dropping a tip/coffee/donation💰
Solution 3:
Using JoinToString
, Checkout advance stuff we can do with it!

fun mummble(words: String): String =
words.mapIndexed { index, word ->
"${word.toUpperCase()}${word.toString().toLowerCase().repeat(index)}"}
.joinToString("-")
// Performance : took ~ 13.525 ms on my machine
Thou, this approach is fancy functional/fluent but have hit due join
Operation.
Community Submission
Contributed by ShreyasPatil
const val DELIMETER_CHAR = "-"
fun main() {
println(mummble("cwAt"))
}
fun mummble(word: String): String {
val mummbleBuilder = StringBuilder()
return with(mummbleBuilder) {
word.forEachIndexed { index, char ->
repeat(index + 1) { count ->
val newChar = if (count == 0) char.toUpperCase() else char.toLowerCase()
append(newChar)
}.also { append(DELIMETER_CHAR) }
}.also { deleteCharAt(mummbleBuilder.lastIndex) }
mummbleBuilder.toString()
}
}
Cons
It's bit more nested, Nesting hurt readability of the code.
Pros
Never know there was a deleteCharAt(index)
function, it delete character at an index on a StringBuilder
object, but has been Deprecated in-favour of deleteAt(index: Int)
, they can throw index-out-of-bound exception
so be careful while using these.
Contributed by Shahbaz

Pros
Cleaner alternative to Solution 3 by using capitalize
reduced the noise related to String Concatenation
Conclusion
Aim of these articles is not hate on Java, but to help people learn various ways in which they can write same logic better and more Kotlin standard library focused way.
Hope you find it informative and if you have any feedback or post request or want to subscribe to my mailing list forms are below.
Until next time. Happy Hacking! 👩💻
Solve Next:

Solve Previous:

Enjoying the Post?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
If you want to support my content do consider dropping a tip/coffee/donation💰