How to print text diagonally from left to right in Kotlin

1 Answer

0 votes
fun printDiagonalTextLTR(text: String): String {
    val builder = StringBuilder()

    text.forEachIndexed { index, ch ->
        builder.append(" ".repeat(index))
        builder.append(ch)
        builder.append('\n')
    }

    return builder.toString()
}

fun main() {
    println(printDiagonalTextLTR("HELLO WORLD"))
}


/*
run:

H
 E
  L
   L
    O
      
      W
       O
        R
         L
          D

*/

 



answered Apr 8 by avibootz
...