w3resource

Java Project - Palindrome Checker


Java - Palindrome Checker using String Reversal:

Palindrome Checker :

Check whether a given word or phrase is a palindrome.

This project checks whether a given word or phrase reads the same backward as forward. The input is a string, and the program removes spaces and punctuation to determine if the string is a palindrome.

Input: A word or phrase.
Output: Whether the input is a palindrome or not.

Example:

  • Input: "madam"
  • Output: "Palindrome"
  • Input: "hello"
  • Output: "Not a palindrome"

Solution 1: Palindrome Checker using String Reversal

Code:

import java.util.Scanner;

public class PalindromeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize scanner to get user input

        System.out.println("Enter a word or phrase to check if it's a palindrome:");
        String input = scanner.nextLine(); // Get the input from the user

        // Remove spaces, punctuation, and convert to lowercase for comparison
        String cleanedInput = input.replaceAll("[^a-zA-Z]", "").toLowerCase(); 

        // Reverse the cleaned input
        String reversedInput = new StringBuilder(cleanedInput).reverse().toString();

        // Check if the cleaned input equals its reversed version
        if (cleanedInput.equals(reversedInput)) {
            System.out.println("Palindrome");
        } else {
            System.out.println("Not a palindrome");
        }

        scanner.close(); // Close the scanner
    }
}

Output:

 Enter a word or phrase to check if it's a palindrome:
madam
Palindrome

Explanation :

  • Input Handling: Uses a scanner to take user input.
  • Cleaning the Input: Removes non-alphabetic characters using regex and converts the string to lowercase.
  • Reversing the String: Uses StringBuilder to reverse the cleaned string.
  • Comparison: Compares the original cleaned string with its reversed version.
  • Output: Prints "Palindrome" if they match, otherwise prints "Not a palindrome".

Solution 2: Palindrome Checker using Two-Pointer Technique

Code:

import java.util.Scanner;

public class PalindromeCheckerTwoPointer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Initialize scanner to get user input

        System.out.println("Enter a word or phrase to check if it's a palindrome:");
        String input = scanner.nextLine(); // Get the input from the user

        // Remove spaces, punctuation, and convert to lowercase for comparison
        String cleanedInput = input.replaceAll("[^a-zA-Z]", "").toLowerCase(); 

        // Use two pointers to compare characters from both ends of the string
        boolean isPalindrome = true; 
        int left = 0; 
        int right = cleanedInput.length() - 1;

        while (left < right) {
            if (cleanedInput.charAt(left) != cleanedInput.charAt(right)) {
                isPalindrome = false; // If characters don't match, it's not a palindrome
                break;
            }
            left++;  // Move the left pointer to the right
            right--; // Move the right pointer to the left
        }

        // Output based on the result
        if (isPalindrome) {
            System.out.println("Palindrome");
        } else {
            System.out.println("Not a palindrome");
        }

        scanner.close(); // Close the scanner
    }
}  

Output:

 Enter a word or phrase to check if it's a palindrome:
madem
Not a palindrome

Explanation:

  • Input Handling: Takes user input through a scanner.
  • Cleaning the Input: Uses regex to remove non-alphabetic characters and converts the string to lowercase.
  • Two-Pointer Technique: Initializes two pointers (left and right) and compares characters from the beginning and end of the cleaned string.
  • Check Characters: If any character comparison fails, it is marked as "Not a palindrome".
  • Output: Prints "Palindrome" if the loop completes without mismatches, otherwise prints "Not a palindrome".

Java Code Editor:




Follow us on Facebook and Twitter for latest update.