String related interview questions Kotlin

Write a kotlin program to reverse a string using recursive methods.


var reverse = ""

fun reverseString(str: String): String {

    if(str.length == 1) {
        return str
    } else {
        reverse += str[str.length - 1] + reverseString(str.substring(0, str.length - 1))
        return reverse
    }
}

fun main(args: Array<String>) {
    var str = "cherchertech"
    println("Original string : "+str)
    println("Reversed string : " + reverseString(str))
}

Output


Original string : cherchertech
Reversed string : hcetrehcrehc

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions