How to check whether a sentence is palindrome in Scala

1 Answer

0 votes
object Palindrome {
  def isSentencePalindrome(str: String): Boolean = {
    // Change the string into lowercase and remove all non-alphanumeric characters
    val cleanedStr = str.toLowerCase.replaceAll("[^a-zA-Z0-9]", "")
    
    cleanedStr == cleanedStr.reverse
  }

  def main(args: Array[String]): Unit = {
    println(if (isSentencePalindrome("Top step's pup's pet spot.")) "yes" else "no")
  }
}


    
    
/*
run:
    
yes
    
*/

 



answered Aug 24, 2024 by avibootz
...