Kotlin Program to Reverse String With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Write a Kotlin Program to reverse string with example

Input:
Hello World

Output:
dlroW olleH
Input:
Hi Ram!

Output:
Reversed: !maR iH

1. Program to Reverse a string

Pseudo Algorithm
  • Use a temp variable result that stores reversed string. Initialize it with empty string.
  • Iterate given string from end index to start start.
  • For each position, get the string at that index and append it in string result
  • Terminate the loop when we reach to start index.
  • Finally, value present in result variable is reversed string.

Sourcecode –

fun main() {

    println("Enter string:")
    val str = readLine()

    var result: String = ""
    var lastIndex = str!!.lastIndex

    while (lastIndex >= 0) {
        result += str[lastIndex]
        lastIndex--
    }

    println("Reversed: $result")
}

When you run the program, output will be –

Enter string:
Hello Rahul! How are you?
Reversed: ?uoy era woH !luhaR olleH
Explanation:
  • Read the value entered by user using readLine() function.
    Then, store this value in variable str
  • Then, define a variable, result that stores the reversed string.
  • Then, get the last index of the string and store it in variable lastIndex.
    We do this to get position of the last character present in string.
  • Then, run a while loop from last index, lastIndex, of the string.
  • Append each character present at index lastIndex in variable result.
  • Then, decrease the value of lastIndex by 1.
  • Repeat the loop till we reach at first character in string.

Let’s take an example,

Assume user entered

string = “hello”

Now,
result = “”
lastIndex = 4

run a loop from 4 till lastIndex becomes 0

  • After iteration at lastIndex = 4,
    result = “o”
    lastIndex = 3
  • After iteration at lastIndex = 3,
    result = “ol”
    lastIndex = 2
  • After iteration at lastIndex = 2,
    result = “oll”
    lastIndex = 1
  • After iteration at lastIndex = 1,
    result = “olle”
    lastIndex = 0
  • After iteration at lastIndex = 0,
    result = “olleh”
    lastIndex = -1

Value at variable result is reversed of variable str.

Thus, we went through Kotlin Program to Reverse a given string.
Similarly, you can use this kotlin program to reverse a sentence.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners