Java Program to Check for a Palindrome

This program checks if a given string is a palindrome. A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

import java.util.Scanner;

public class PalindromeChecker {

    // Method to check if a string is a palindrome
    public static boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;

        // Loop to compare characters from both ends
        while (left < right) {
            // If characters do not match, it's not a palindrome
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    // Main method
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input: User enters a string
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Check if the input string is a palindrome
        boolean result = isPalindrome(input);

        // Output: Display the result
        if (result) {
            System.out.println(input + " is a palindrome.");
        } else {
            System.out.println(input + " is not a palindrome.");
        }
    }
}

Code Breakdown

1. Import Statement

  • Line: import java.util.Scanner;
  • This line imports the Scanner class from the java.util package to get user input.

2. Class Declaration

  • Line: public class PalindromeChecker {
  • This line declares a public class named PalindromeChecker.

3. Method to Check for Palindrome

  • Line: public static boolean isPalindrome(String str) {
  • This defines a method that checks if a string is a palindrome and returns a boolean value.

4. Initialize Pointers

int left = 0;
int right = str.length() - 1;
  • left starts at the beginning, and right starts at the end of the string.

5. Loop to Compare Characters

  • Line: while (left < right) {
  • This loop continues as long as left is less than right.

6. Character Comparison

if (str.charAt(left) != str.charAt(right)) {
    return false;
}
  • This checks if characters at left and right indices are different. If they are, it returns false.

7. Move Pointers

left++;
right--;
  • If the characters match, increment left and decrement right.

8. Return True

  • Line: return true;
  • If the loop completes without finding mismatches, return true.

Main Method

9. Create Scanner Object

  • Line: Scanner scanner = new Scanner(System.in);
  • This creates a Scanner object for reading user input.

10. User Input

System.out.print("Enter a string: ");
String input = scanner.nextLine();
  • Prompts the user for input and reads it into the variable input.

11. Check for Palindrome

  • Line: boolean result = isPalindrome(input);
  • Calls the isPalindrome method with the user’s input.

12. Display the Result

if (result) {
    System.out.println(input + " is a palindrome.");
} else {
    System.out.println(input + " is not a palindrome.");
}
  • Checks the result and prints whether the input is a palindrome.

Conclusion

This program effectively checks whether a given string is a palindrome by comparing characters from both ends of the string. It utilizes fundamental programming concepts such as methods, loops, conditionals, and user input. If you have any questions or need clarification on any part, feel free to ask!



0 Comments

Leave a Reply

Avatar placeholder

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