Java Program for Palindrome

palindrome java program
Reading Time: 7 minutes

Introduction:

The provided Java program is a Palindrome Checker. It determines whether a given input string is a palindrome or not. A palindrome is a sequence of characters that reads the same forward as backward, ignoring spaces and considering characters in a case-insensitive manner. The program uses a simple console interface to take user input, checks if the input is a palindrome, and provides the result.

Explanation:

  1. User Input:
    • The program starts by creating a Scanner object to take input from the user.
    • The user is prompted to enter a string.
  2. Palindrome Checking:
    • The program then calls the isPalindrome method to check if the entered string is a palindrome.
    • The isPalindrome method performs the following steps:
      • It removes spaces from the input string using replaceAll(“\\s”, “”).
      • It converts the string to lowercase using toLowerCase() for case-insensitive comparison.
      • It initializes two pointers, left and right, pointing to the start and end of the cleaned string.
      • It uses a while loop to compare characters at the left and right
        • If the characters are not equal, the method returns false indicating that the string is not a palindrome.
        • The pointers are then updated to check the next pair of characters.
      • If the loop completes without returning false, the method returns true, indicating that the string is a palindrome.
  1. Output:
    • Depending on the result of the palindrome check, the program prints whether the entered string is a palindrome or not.
  2. Resource Management:
    • The program closes the Scanner object to free up system resources after obtaining the input.

Algorithm:

The algorithm used for palindrome checking involves the following steps:

  1. Remove spaces and convert the string to lowercase.
  2. Initialize two pointers, left and right, at the start and end of the cleaned string.
  3. Compare characters at the left and right
  4. If characters are not equal, return false.
  5. Update pointers and repeat the comparison until left is greater than or equal to right.
  6. If the loop completes without returning false, return true indicating a palindrome.

Code:

				
					import java.util.Scanner;
public class PalindromeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        if (isPalindrome(input)) {
            System.out.println(input + " is a palindrome!");
        } else {
            System.out.println(input + " is not a palindrome.");
        }
        scanner.close();
    }
    public static boolean isPalindrome(String str) {
        // Remove spaces and convert to lowercase for case-insensitive comparison
        String cleanedStr = str.replaceAll("\\s", "").toLowerCase();
        int left = 0;
        int right = cleanedStr.length() - 1;
        while (left < right) {
            if (cleanedStr.charAt(left) != cleanedStr.charAt(right)) {
                return false; // Not a palindrome
            }
            left++;
            right--;
        }

        return true; // It's a palindrome
    }
}

				
			

Input :

				
					Enter a string: tamil
				
			

Output :

				
					tamil is not a palindrome.
				
			

This program takes a string as input, removes spaces, and converts it to lowercase to perform a case-insensitive comparison. It then checks if the string is a palindrome and outputs the result.

FAQs

1) Q: What is a common Java program to check if a string is a palindrome?

A: You can use a Java program that compares characters from both ends after removing spaces and converting to lowercase. See the example above.

2) Q: How can I create a Java program for palindrome checking?

A: You can write a Java program by taking a string input, removing spaces, and comparing characters from both ends using pointers. Check the provided Java code for a detailed example.

3) Q: Is there a specific Java program for palindrome strings?

A: Yes, you can use a Java program specifically designed to check if a given string is a palindrome. It involves removing spaces and comparing characters without considering case.

4) Q: Can you provide a Java program to check whether a string is a palindrome or not?

A: Certainly! The Java program provided above is designed to efficiently check whether a given string is a palindrome, handling spaces and case-insensitivity.

5) Q: How does a Java program check if a string is a palindrome?

A: A Java program typically removes spaces, converts the string to lowercase, and compares characters from both ends using pointers to determine if it’s a palindrome.

6) Q: What is the significance of a Java program for palindrome strings?

A: A Java program for palindrome strings is essential for determining whether a given sequence of characters reads the same forwards and backwards, aiding in various applications like data validation and algorithmic problems.