Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

Java Program to Check Whether a Given String is Palindrome or Not

Posted on July 15, 2025July 15, 2025 By Admin No Comments on Java Program to Check Whether a Given String is Palindrome or Not

A palindrome is a string that reads the same forward and backward. Examples include "madam", "level", and "racecar".

In this article, we’ll explore multiple approaches in Java to determine if a given string is a palindrome. Each method will consider edge cases like case sensitivity and special characters.

What is a Palindrome?

A string is a palindrome if:

  • The first character is the same as the last,
  • The second is the same as the second last,
  • … and so on.

Examples:

InputPalindrome?
“madam”✅ Yes
“hello”❌ No
“Level”✅ Yes (case-insensitive)
“Was it a car or a cat I saw?”✅ Yes (if ignoring spaces/punctuation)

Approach 1: Reverse the String and Compare

Reverse the string and compare it with the original.

// Java Program to Check the Given String
// is Palindrome or not

public class PalindromeCheck {
    public static void main(String[] args) {
        String str = "madam";
        String reversed = new StringBuilder(str).reverse().toString();

        if (str.equals(reversed)) {
            System.out.println("The string is a palindrome.");
        } else {
            System.out.println("The string is not a palindrome.");
        }
    }
}

Approach 2: Character-by-Character Comparison

Use two pointers (start and end) and compare characters one by one.

// Java Program to Check the Given String
// is Palindrome or not

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;

        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "level";
        System.out.println("Is palindrome: " + isPalindrome(str));
    }
}

Approach 3: Case-Insensitive Check

Convert the string to lowercase to ignore case.

// Java Program to Check the Given String
// is Palindrome or not

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        str = str.toLowerCase(); // or str.toUpperCase()
        int left = 0, right = str.length() - 1;

        while (left < right) {
            if (str.charAt(left++) != str.charAt(right--)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "Level";
        System.out.println("Is palindrome: " + isPalindrome(str));
    }
}

Approach 4: Ignoring Non-Alphanumeric Characters

Normalize the string by removing spaces and punctuation.

// Java Program to Check the Given String
// is Palindrome or not

public class PalindromeCheck {
    public static boolean isPalindrome(String str) {
        str = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        int left = 0, right = str.length() - 1;

        while (left < right) {
            if (str.charAt(left++) != str.charAt(right--)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "A man, a plan, a canal, Panama!";
        System.out.println("Is palindrome: " + isPalindrome(str));
    }
}

Approach 5: Recursive Solution

Use recursion to compare the first and last characters.

// Java Program to Check the Given String
// is Palindrome or not

public class PalindromeCheck {
    public static boolean isPalindrome(String str, int left, int right) {
        if (left >= right) return true;
        if (str.charAt(left) != str.charAt(right)) return false;
        return isPalindrome(str, left + 1, right - 1);
    }

    public static void main(String[] args) {
        String str = "radar";
        boolean result = isPalindrome(str, 0, str.length() - 1);
        System.out.println("Is palindrome: " + result);
    }
}
Java, Programs Tags:Java-Programs, Java-Strings

Post navigation

Previous Post: Java Program to Find the Second Largest Element of an Array
Next Post: Selenium – A Web Application Testing Framework

Leave a Reply Cancel reply

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

Archives

  • July 2025
  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Selenium – A Web Application Testing Framework
  • Java Program to Check Whether a Given String is Palindrome or Not
  • Java Program to Find the Second Largest Element of an Array
  • Adobe Interview Experience | Senior QA Engineer | Automation Test for Noida Office [Latest 2025]
  • Java Program to Find the Largest Element of an Array

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme