A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization. In Java, checking whether a string is a palindrome involves comparing the original string with its reversed version.
A string is considered a palindrome if it reads the same backward as forward. For example, "madam" and "racecar" are palindromes. This property is utilized in various applications, including data validation and cryptography.
Java provides a robust set of string operations that can be leveraged to check for palindromes:
Below is a Java program that checks if a given string is a palindrome:
import java.util.Scanner; public class PalindromeCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt user for input System.out.print("Enter a string: "); String input = scanner.nextLine(); // Remove spaces and convert to lowercase String cleanedInput = input.replaceAll("\\s+", "").toLowerCase(); // Initialize a StringBuilder to reverse the cleaned input StringBuilder reversedInput = new StringBuilder(cleanedInput); reversedInput.reverse(); // Check if the cleaned input is equal to its reverse if (cleanedInput.equals(reversedInput.toString())) { System.out.println(input + " is a palindrome."); } else { System.out.println(input + " is not a palindrome."); } scanner.close(); } }
Alternatively, you can check for a palindrome by comparing characters from both ends of the string:
public class PalindromeCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt user for input System.out.print("Enter a string: "); String input = scanner.nextLine(); // Remove spaces and convert to lowercase String cleanedInput = input.replaceAll("\\s+", "").toLowerCase(); // Initialize a flag to indicate palindrome status boolean isPalindrome = true; // Compare characters from both ends for (int i = 0; i < cleanedInput.length() / 2; i++) { if (cleanedInput.charAt(i) != cleanedInput.charAt(cleanedInput.length() - 1 - i)) { isPalindrome = false; break; } } // Output the result if (isPalindrome) { System.out.println(input + " is a palindrome."); } else { System.out.println(input + " is not a palindrome."); } scanner.close(); } }
A palindrome string is a sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization. Examples include "madam" and "racecar".
You can check if a string is a palindrome in Java by reversing the string and comparing it to the original string. Alternatively, you can compare characters from both ends of the string moving towards the center.
Yes, Java strings are case-sensitive. To perform a case-insensitive palindrome check, convert the string to lowercase or uppercase before comparison.
You can remove all whitespace characters from a string in Java using the replaceAll("\\s+", "") method, which replaces all occurrences of one or more whitespace characters with an empty string.
Yes, a single character is trivially a palindrome because it reads the same forward and backward.
Copyrights © 2024 letsupdateskills All rights reserved