what is a palindrome?

Exploring Palindrome Programs in Java: Unraveling the Symmetry

Introduction: Palindrome programs in Java hold a certain fascination as they involve creating sequences of characters that read the same forward and backward. This symmetric quality makes palindromes an interesting and valuable concept in programming. In this blog, we will delve into the concept of palindromes and present two examples of Java programs that check whether a given string is a palindrome or not.

Understanding Palindromes: A palindrome is a sequence of characters that reads the same forward and backward. In the context of programming, this often refers to words, phrases, or entire sentences. Palindromes exhibit a unique symmetry that makes them a captivating topic for programmers.

Example 1: Palindrome Program Using String Reversal

import java.util.Scanner;

public class PalindromeExample1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        scanner.close();

        // Removing spaces and converting to lowercase for case-insensitivity
        String processedInput = input.replaceAll("\\s", "").toLowerCase();

        // Reversing the string
        String reversedInput = new StringBuilder(processedInput).reverse().toString();

        // Checking if the reversed string is equal to the original string
        if (processedInput.equals(reversedInput)) {
            System.out.println(input + " is a palindrome!");
        } else {
            System.out.println(input + " is not a palindrome.");
        }
    }
}

Example 2: Palindrome Program Using Two Pointers

import java.util.Scanner;

public class PalindromeExample2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        scanner.close();

        // Removing spaces and converting to lowercase for case-insensitivity
        String processedInput = input.replaceAll("\\s", "").toLowerCase();

        // Using two pointers to check for palindrome
        int start = 0;
        int end = processedInput.length() - 1;
        boolean isPalindrome = true;

        while (start < end) {
            if (processedInput.charAt(start) != processedInput.charAt(end)) {
                isPalindrome = false;
                break;
            }
            start++;
            end--;
        }

        // Displaying the result
        if (isPalindrome) {
            System.out.println(input + " is a palindrome!");
        } else {
            System.out.println(input + " is not a palindrome.");
        }
    }
}

Conclusion: Palindromes, with their intriguing symmetry, provide an excellent opportunity for programmers to explore string manipulation and algorithmic techniques. The presented Java programs showcase different approaches to checking whether a given string is a palindrome or not. As you experiment with these programs, you'll gain a deeper understanding of string operations and logic, enhancing your programming skills.