Java Program to Check Palindrome String

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

A palindrome is a string that reads the same forward and backward. For example, "madam" and "racecar" are palindromes. This guide will show you how to create a Java program that checks whether a given string is a palindrome.

Problem Statement

Create a Java program that:

  • Takes a string as input.
  • Checks if the string is a palindrome.
  • Displays the result indicating whether the string is a palindrome.

Example 1:

  • Input: "madam"
  • Output: "madam" is a palindrome.

Example 2:

  • Input: "hello"
  • Output: "hello" is not a palindrome.

Approach 1: Using a for Loop

Java Program

import java.util.Scanner; /** * Java Program to Check Palindrome String using for loop * Author: https://www.javaguides.net/ */ public class PalindromeCheckForLoop { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input System.out.print("Enter a string to check if it is a palindrome: "); String input = scanner.nextLine(); // Step 2: Check if the string is a palindrome using a for loop boolean isPalindrome = isPalindrome(input); // Step 3: Display the result if (isPalindrome) { System.out.println("\"" + input + "\" is a palindrome."); } else { System.out.println("\"" + input + "\" is not a palindrome."); } } // Method to check if a string is a palindrome using a for loop public static boolean isPalindrome(String str) { int n = str.length(); for (int i = 0; i < n / 2; i++) { if (str.charAt(i) != str.charAt(n - i - 1)) { return false; } } return true; } } 

Explanation

  • Input: The program prompts the user to enter a string.
  • Palindrome Check: A for loop is used to compare characters from the beginning and end of the string, moving toward the center. If any characters don't match, the string is not a palindrome.
  • Output: The program prints whether the string is a palindrome or not.

Output Example

Enter a string to check if it is a palindrome: madam "madam" is a palindrome. 
Enter a string to check if it is a palindrome: hello "hello" is not a palindrome. 

Approach 2: Using StringBuilder's reverse() Method

Java Program

import java.util.Scanner; /** * Java Program to Check Palindrome String using StringBuilder * Author: https://www.javaguides.net/ */ public class PalindromeCheckStringBuilder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input System.out.print("Enter a string to check if it is a palindrome: "); String input = scanner.nextLine(); // Step 2: Check if the string is a palindrome using StringBuilder's reverse method String reversed = new StringBuilder(input).reverse().toString(); // Step 3: Display the result if (input.equals(reversed)) { System.out.println("\"" + input + "\" is a palindrome."); } else { System.out.println("\"" + input + "\" is not a palindrome."); } } } 

Explanation

  • Input: The program prompts the user to enter a string.
  • Palindrome Check: The input string is reversed using the StringBuilder's reverse() method. The original string is then compared to the reversed string.
  • Output: The program prints whether the string is a palindrome or not.

Output Example

Enter a string to check if it is a palindrome: racecar "racecar" is a palindrome. 
Enter a string to check if it is a palindrome: world "world" is not a palindrome. 

Approach 3: Using Recursion

Java Program

import java.util.Scanner; /** * Java Program to Check Palindrome String using Recursion * Author: https://www.javaguides.net/ */ public class PalindromeCheckRecursion { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt the user for input System.out.print("Enter a string to check if it is a palindrome: "); String input = scanner.nextLine(); // Step 2: Check if the string is a palindrome using recursion boolean isPalindrome = isPalindrome(input, 0, input.length() - 1); // Step 3: Display the result if (isPalindrome) { System.out.println("\"" + input + "\" is a palindrome."); } else { System.out.println("\"" + input + "\" is not a palindrome."); } } // Recursive method to check if a string is a palindrome public static boolean isPalindrome(String str, int left, int right) { if (left >= right) { return true; // Base case: all characters have been checked } if (str.charAt(left) != str.charAt(right)) { return false; // Characters do not match } return isPalindrome(str, left + 1, right - 1); // Recurse for next characters } } 

Explanation

  • Input: The program prompts the user to enter a string.
  • Palindrome Check: A recursive method checks if the string is a palindrome by comparing the first and last characters, then moving inward. If all characters match, the string is a palindrome.
  • Output: The program prints whether the string is a palindrome or not.

Output Example

Enter a string to check if it is a palindrome: level "level" is a palindrome. 
Enter a string to check if it is a palindrome: example "example" is not a palindrome. 

Conclusion

This Java program provides multiple methods to check if a string is a palindrome, demonstrating different techniques such as loops, StringBuilder, and recursion. Each approach is useful in different contexts, and the choice of method may depend on the specific requirements, such as readability, performance, or coding style. These methods are commonly used in various string manipulation tasks in Java.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare