Monday, December 21, 2020

Java Program To Check Palindrome String Using Recursion

Oracle Java Tutorial and Material, Oracle Java Certifications, Java Exam Prep, Oracle Java Career

1. Overview

In this tutorial, We’ll learn how to check the string is palindrome using Recursive function in java.

String palindrome means if the string reversed value is equal to the original string.

Recursion means calling the one function from the same function. This is a computer programming concept and it is implemented in java.

2. Java String Palindrome Recursive example

Below example code is implemented using recursion approach. In that isPalindrome() method is getting called from the same method with substring value of original string.

Example: 

Step 1: Input string is : madam

Step 2: First call to isPalindrome(“madam”) – first and last character is same -> m == m -> true

Step 3: Next, calling the same isPalindrome(“ada”) with “ada” – first and last character same -> a == a -> true

Step 4: Next, again calling the same method isPalindrome() with “d” value. Now the first if condition checks that length of this string is 1 so it returns true. This value passed to the previous step where input is “ada”.

Step 5: Again from “ada” call returns true value to “madam” input call. So, finally it returns true to the main method.

All of these methods are placed inside a Runtime Stack and will be called back from top.

If the input is 1234, first and last character is not same so it comes to final return false. Hence, the string 1234 is not a palindrome.

package com.javaprogramto.programs.strings;

public class StringPalindromeRecursiveExample {

    public static void main(String[] args) {

        // input string 1234

        String string = "1234";

        /*

         * If function returns true then the string is palindrome otherwise not

         */

        if (isPalindrome(string))

            System.out.println(string + " is a palindrome");

        else

            System.out.println(string + " is not a palindrome");

        // input string 1234

        String string2 = "madam";

        /*

         * If function returns true then the string is palindrome otherwise not

         */

        if (isPalindrome(string2))

            System.out.println(string2 + " is a palindrome");

        else

            System.out.println(string2 + " is not a palindrome");

 

    }

    /**

     * Recursive function to check the string is palindrome or not.

     * 

     * @param s

     * @return

     */

    public static boolean isPalindrome(String s) {

        // if the string has one or zero characters then recursive call is stopped.

        if (s.length() == 0 || s.length() == 1)

            return true;

        // checking the first and last character of the string. if equals then call the

        // same function with substring from index 1 to length -1. Because substring

        // excludes the endIndex.

        // if these two values are not same then string is not Palindrome so this

        // returns false.

        if (s.charAt(0) == s.charAt(s.length() - 1))

            return isPalindrome(s.substring(1, s.length() - 1));

        // this statment is executed if and if only first and last character of string

        // at any time is not equal.

        return false;

    }

}

Output:

1234 is not a palindrome

madam is a palindrome

Related Posts

0 comments:

Post a Comment