0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Palindrome String

You can check a palindrome in Kotlin by comparing a string with its reversed version using val isPalindrome = 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 word is a palindrome, think about reading it from left to right and right to left. If both readings are the same, the word 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 it is a palindrome.
5
Otherwise, print that it is not a palindrome.
💻

Code

kotlin
fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    val isPalindrome = input == input.reversed()
    if (isPalindrome) {
        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

Output: 'madam is a palindrome'

StepInputReversedComparison Result
1madammadamtrue
💡

Why This Works

Step 1: Reading input

We get the string from the user to check if it is a palindrome.

Step 2: Reversing the string

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

Step 3: Comparing strings

If the original string and reversed string are exactly the same, the string is a palindrome.

Step 4: Output result

Print a message telling the user if the string is a palindrome or not.

🔄

Alternative Approaches

Manual character comparison
kotlin
fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    var isPalindrome = true
    val length = input.length
    for (i in 0 until length / 2) {
        if (input[i] != 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 from both ends without creating a reversed string, saving memory for large inputs.
Using recursion
kotlin
fun isPalindrome(s: String): Boolean {
    if (s.length <= 1) return true
    if (s.first() != s.last()) return false
    return isPalindrome(s.substring(1, s.length - 1))
}

fun main() {
    print("Enter a string: ")
    val input = readLine() ?: ""
    if (isPalindrome(input)) {
        println("$input is a palindrome")
    } else {
        println("$input is not a palindrome")
    }
}
This recursive approach is elegant but may be less efficient for very long strings due to function call overhead.

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

Time Complexity

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

Space Complexity

The reversed string creates a new string of length n, so space complexity is O(n). The manual comparison method uses O(1) extra space.

Which Approach is Fastest?

Manual character comparison is faster and uses less memory than reversing the string, especially for large inputs.

ApproachTimeSpaceBest For
Using reversed()O(n)O(n)Simplicity and readability
Manual character comparisonO(n)O(1)Memory efficiency and large inputs
RecursionO(n)O(n)Elegant code but less efficient for large strings
💡
Use input == input.reversed() for a quick and readable palindrome check in Kotlin.
⚠️
Beginners often forget to handle empty or single-character strings, which are palindromes by definition.