Kotlin Program:

- To reverse a string

Example:

User Input: Hello World

Output: dlroW olleH

import java.util.Scanner
fun main(args: Array<String>) {
  print("Enter your string : ")
  val userInput = Scanner(System.`in`)
  var s1:String
  var reverse:String=""
  s1=userInput.nextLine()
  var l:Int=s1.length-1

  while(l!=-1) {
    reverse+=s1[l--];
  }
  //reverse=s1.reversed();
  println("Your reversed string : $reverse")
}

Output:

  Enter Your string : fun with code
  Your reversed string : edoc htiw nuf
Program
string

Main Logic :

// varibale 'l' holds length of string - 1 (since index starts from 0)
while(l!=-1) {
  reverse+=s1[l--];
}

OR, You can use predefined function

s1.reversed();
Concept

Coming Soon !

Quick
Tutorial

Quote

Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.