0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Palindrome String

You can check if a string is a palindrome in Kotlin by comparing it with its reversed version using input == input.reversed().
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome, think of reading the string from left to right and from right to left. If both readings are exactly the same, the string is a palindrome. So, reverse the string and compare it with the original string using ==.
📐

Algorithm

1
Get the input string from the user.
2
Reverse the input string.
3
Compare the original string with the reversed string.
4
If both are equal, print that the string is a palindrome.
5
Otherwise, print that it is not a palindrome.
💻

Code

kotlin
fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    if (input == input.reversed()) {
        println("$input is a palindrome")
    } else {
        println("$input is not a palindrome")
    }
}
Output
Enter a string: madam madam is a palindrome
🔍

Dry Run

Let's trace the input 'madam' through the code

1

Read input

input = 'madam'

2

Reverse input

input.reversed() = 'madam'

3

Compare strings

'madam' == 'madam' is true

4

Print result

Print 'madam is a palindrome'

StepInputReversedComparison Result
1madammadamtrue
💡

Why This Works

Step 1: Reading input

We get the string from the user using readLine() which reads text from the keyboard.

Step 2: Reversing the string

Using input.reversed() creates a new string with characters in reverse order.

Step 3: Comparing strings

We use == to check if the original string and reversed string are exactly the same.

Step 4: Output result

If they match, we print that the string is a palindrome; otherwise, we say it is not.

🔄

Alternative Approaches

Manual character comparison
kotlin
fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    var isPalindrome = true
    for (i in 0 until input.length / 2) {
        if (input[i] != input[input.length - 1 - i]) {
            isPalindrome = false
            break
        }
    }
    if (isPalindrome) {
        println("$input is a palindrome")
    } else {
        println("$input is not a palindrome")
    }
}
This method checks characters one by one from start and end, which can be more efficient for large strings because it stops early if mismatch found.
Using Kotlin extension function
kotlin
fun String.isPalindrome(): Boolean = this == this.reversed()

fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    if (input.isPalindrome()) {
        println("$input is a palindrome")
    } else {
        println("$input is not a palindrome")
    }
}
This approach makes the palindrome check reusable and clean by adding a function to the String class.

Complexity: O(n) time, O(n) space

Time Complexity

Reversing the string takes O(n) time where n is the string length, and comparing two strings also takes O(n). Overall, it is O(n).

Space Complexity

The reversed string creates a new string of length n, so space complexity is O(n).

Which Approach is Fastest?

Manual character comparison can be faster in practice because it stops early on mismatch and uses O(1) extra space.

ApproachTimeSpaceBest For
Using reversed()O(n)O(n)Simple and readable code
Manual character comparisonO(n) best case lessO(1)Large strings with early mismatch
Extension functionO(n)O(n)Reusable and clean code
💡
Use input == input.reversed() for a quick palindrome check in Kotlin.
⚠️
Beginners often forget to handle null or empty input which can cause errors or wrong results.