public class PalindromeChecker {
public static void main(String[] args) {
System.out.println(isPalindrome("A man, a plan, a canal, Panama")); // true
System.out.println(isPalindrome("racecar")); // true
System.out.println(isPalindrome("hello")); // false
}
public static boolean isPalindrome(String str) {
// Remove spaces and punctuation, and convert to lowercase
String cleanedStr = str.replaceAll("[^a-zA-Z]", "").toLowerCase();
// Compare characters from both ends of the cleaned string
int left = 0;
int right = cleanedStr.length() - 1;
while (left < right) {
if (cleanedStr.charAt(left) != cleanedStr.charAt(right)) {
return false; // Characters don't match, not a palindrome
}
left++;
right--;
}
return true; // All characters matched, it's a palindrome
}
}