How to check if a string is a palindrome ignoring case and non-alphanumeric characters in Kotlin

1 Answer

0 votes
fun isPalindrome(s: String): Boolean {
    // Remove non-alphanumeric characters and convert to lowercase
    val normalized = s.replace(Regex("[^a-zA-Z0-9]"), "").lowercase()
    println(normalized)

    // Check if the string is equal to its reverse
    return normalized == normalized.reversed()
}

fun main() {
    val s = "+^-Ab#c!D 50...#  05*()dcB[]A##@!$"
    
    println(isPalindrome(s))
}


 
  
/*
run:

abcd5005dcba
true

*/

 



answered Aug 10, 2025 by avibootz
...