Scala Program to Check Palindrome

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    In this article, we are going to learn about the term palindrome and how we can check if input data is palindrome or not. We will cover it for two data types: String and Integer . Before proceeding further in this article, you should be aware of the below concepts:

    1. How to take user input from the keyboard?
    2. What is tail recursion?
    3. Concept of nested function.

    All the above three concepts will be used in the implementation of a palindrome program. If you are already aware of the concepts, you can easily implement the program.

    Let us begin by understanding the definition of the palindrome.

    What is a Palindrome?

    It is a word, phrase, number, or sequence when read backward or forward, produces the same result. In other words, if the reverse of a number/string is the same as the original number/string, it is termed a palindrome number/string.

    Examples of Palindrome string: Madam, racecar, radar, level, etc.

    Examples of palindrome numbers: 123321, 12321, 1221, 121, 565, etc.

    Program 1: Scala Program to Check Whether a Given Number is Palindrome

    objectPalindromeextendsApp {
    
    defcheckPalindrome(number: Int): Boolean = {
    
    defcomputeReverse(number:Int, reverse: Int = 0): Int = {
    if (number == 0) {
    reverse
    } else {
    computeReverse(number/10, (reverse * 10) + (number % 10))
    }
    }
    
    val reverse = computeReverse(number)
    if (number == reverse) {
    true
    } else {
    false
    }
    }
    
    println("Please enter a number to check palindrome")
    val number = scala.io.StdIn.readInt()
    
    if(checkPalindrome(number)) {
    println(s"${number} is a palindrome")
    } else {
    println(s"${number} is not a palindrome")
    }
    
    }

    Output:

    Sample Output 1:

    Please enter a number to check palindrome
    12321
    12321 is a palindrome

    Sample Output 2:

    Please enter a number to check palindrome
    1234
    1234 is not a palindrome

    Program 2: Scala Program to Check Whether a Given String is Palindrome

    objectPalindromeextendsApp {
    
    defcheckPalindrome(word: String): Boolean = {
    val reverse = word.reverse
    if (word == reverse) {
    true
    } else {
    false
    }
    }
    
    println("Please enter a word to check palindrome")
    val word = scala.io.StdIn.readLine()
    
    if(checkPalindrome(word)) {
    println(s"${word} is a palindrome")
    } else {
    println(s"${word} is not a palindrome")
    }
    
    }
    
    

    Output:

    Sample Output 1:

    Please enter a word to check palindrome
    abcabc
    abcabc is not a palindrome

    Sample Output 2:

    Please enter a word to check palindrome
    aba
    aba is a palindrome

    Explanation

    In both the above programs, we first asked the user to input the data to check if it is palindrome or not. We validated the input for palindrome by checking the input data with the reverse of the entered input. If they were the same, the input data was a palindrome; otherwise, not.

    The only difference that lies in the above two programs is the way of evaluating the reverse of the input data. In the case of String, we utilized the method provided by the String class to find out the reverse of a string. In the case of the Integer type, we wrote our own recursive function to calculate the reverse of a given number.

    Conclusion

    I hope the above programs will help you understand the concept of a palindrome. You can try out the above example by replacing the string reverse() method and creating your own implementation to find out the reverse of a string.

    Happy Coding!

    Leave a Comment on this Post

    0 Comments