BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

java program to check palindrome string using recursion

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

Program: Check whether String is palindrome using recursion

package beginnersbook.com;
import java.util.Scanner;
class PalindromeCheck
{
    //My Method to check
    public static boolean isPal(String s)
    {   // if length is 0 or 1 then String is palindrome
        if(s.length() == 0 || s.length() == 1)
            return true; 
        if(s.charAt(0) == s.charAt(s.length()-1))
        /* check for first and last char of String:
         * if they are same then do the same thing for a substring
         * with first and last char removed. and carry on this
         * until you string completes or condition fails
         * Function calling itself: Recursion
         */
        return isPal(s.substring(1, s.length()-1));

        /* If program control reaches to this statement it means
         * the String is not palindrome hence return false.
         */
        return false;
    }

    public static void main(String[]args)
    {
    	//For capturing user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the String for check:");
        String string = scanner.nextLine();
        /* If function returns true then the string is
         * palindrome else not
         */
        if(isPal(string))
            System.out.println(string + " is a palindrome");
        else
            System.out.println(string + " is not a palindrome");
    }
}

Output:

Enter the String for check:
qqaabb
qqaabb is not a palindrome

Output 2:

Enter the String for check:
cocoococ
cocoococ is a palindrome

Top Related Articles:

  1. java program to find factorial of a given number using recursion
  2. Java Program to Calculate average using Array
  3. Java Program to find Sum of Natural Numbers
  4. Java Program to find all subsets of a string
  5. Tech Number Program in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Avi says

    February 11, 2015 at 1:10 AM

    Can you add an ignorecase to your program to ensure that something in capital letters still can be a palindrome like Racecar

    Reply
    • joseph says

      March 8, 2016 at 8:41 PM

      thanks, for giving me a project to turn in :)

      Reply
  2. Theodora Baxevani says

    July 1, 2015 at 2:00 PM

    Hello friends!I would like to ask why you use length() instead of .length

    Reply
    • Jaideep says

      February 19, 2017 at 3:07 PM

      because it is the need to hava a null value as an argument to a method! In short, it is what java requires!

      Reply
  3. amit saini says

    October 13, 2015 at 9:26 AM

    .length is used for to find the length of a array. and the method length() is used for to find the length of a String

    Reply
    • Theodora Baxevani says

      February 5, 2016 at 11:08 PM

      Well thank you a lot amit saini !

      Reply
  4. Learner says

    July 4, 2016 at 3:55 AM

    Could you please explain how substring function has worked in this program? Also why is it necessary to return isPal, why cant we simply call isPal? (I know it gives incorrect result when isPal is just called, but wanted to know how the ‘return isPal’ statement is working in this program)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap