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

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions