1. Introduction

Reversing a string is a common programming task that can be done in many languages, including Kotlin. Simply put, to reverse a string means to rewrite the same string backward. For instance, “this is a sentence” when reversed is “ecnetnes a si siht”.

In this tutorial, we’ll explore different methods to reverse a string in Kotlin, along with sample code and unit tests.

2. Programmatic Approach

We can write a custom method to reverse a string that accepts a String as a parameter. This method will iterate the characters of the string in reverse order while appending each character to a new String with a for() loop. Lastly, it returns this new string as the result:

fun reverseSentence(sentence: String): String {
    var reversedSentence = ""
    for (i in sentence.length - 1 downTo 0) {
        reversedSentence += sentence[i]
    }
    return reversedSentence
}

Now, let’s test our method for correctness:

@Test
fun `reverse sentence using custom method`() {
    val sentence = "this is a sentence"
    val expected = "ecnetnes a si siht"

    assertEquals(expected, reverseSentence(sentence))
}

2. Using the String.reversed() Method

Another interesting way to reverse a string is by using Kotlin’s own String.reversed() method. This function directly returns the reverse of the String:

@Test
fun `reverse sentence using string reversed() method`() {
    val sentence = "this is a sentence"
    val expected = "ecnetnes a si siht"

    assertEquals(expected, sentence.reversed())
}

3. Using the StringBuilder.reverse() Method

We can also use the StringBuilder.reverse() method to reverse a string in Kotlin. Similarly, this method accepts a string parameter and creates a new StringBuilder. Subsequently, it calls reverse() on the StringBuilder and is finally able to return the reversed String:

@Test
fun `reverse sentence using stringbuilder reverse() method`() {
    val sentence = "this is a sentence"
    val expected = "ecnetnes a si siht"

    assertEquals(expected, StringBuilder(sentence).reverse().toString())
}

4. Using Recursion

Finally, we can also use recursion to reverse a string. We’ll start with a function that accepts a String and recursively reverses it. First, our base case checks if the string is empty, and if yes, returns an empty string. Otherwise, we’ll move the first character to the end and then recursively call our function on the rest of the string starting from the second character. This process continues until the entire text is reversed:

fun reverseSentenceRecursively(sentence: String): String {
    return if (sentence.isEmpty()) {
        ""
    } else {
        reverseSentenceRecursively(sentence.substring(1)) + sentence[0]
    }
}

As usual, let’s make sure our method works as expected:

@Test
fun `reverse sentence using recursion method`() {
    val sentence = "this is a sentence"
    val expected = "ecnetnes a si siht"
        
    assertEquals(expected, reverseSentenceRecursively(sentence))
}

5. Conclusion

In this article, we’ve explored different methods to reverse a string in Kotlin. We’ve looked at several built-in methods to achieve this, such as the StringBuilder.reverse() and String.reversed() methods. We’ve also written our own custom methods to demonstrate programmatic and recursive approaches. We could always adopt any of these methods based on our project needs.

As always, the code samples and relevant test cases pertaining to this article can be found over on GitHub.