Kotlin Palindrome Program

Kotlin – Palindrome Program

A palindrome is a word, phrase, or sequence of characters that reads the same forwards and backwards. To check if a string is a palindrome in Kotlin, you can write a simple program.

Inside the program,

  1. We are given a string value in inputString.
  2. Find the reverse of the inputString, and store the result in reversedInputString.
  3. Write an if statement to check if inputString and reversedInputString are equal. If they are equal, the given string is a Palindrome string, otherwise not.

Main.kt

fun main() {
    val inputString = "racecar" // Change this to the string you want to check

    val reversedInputString = inputString.reversed()

    if (inputString == reversedInputString) {
        println("Palindrome string.")
    } else {
        println("Not a Palindrome string.")
    }
}

Output

Palindrome string.

Now, let us take a different value in inputString, say, "apple" and check if it is Palindrome string.

Main.kt

fun main() {
    val inputString = "apple"

    val reversedInputString = inputString.reversed()

    if (inputString == reversedInputString) {
        println("Palindrome string.")
    } else {
        println("Not a Palindrome string.")
    }
}

Output

Not a Palindrome string.

Conclusion

In this Kotlin Programs tutorial, we have seen how to check if a given string is a Palindrome string or not.