Write a program that takes a string as input and determines whether it's a palindrome or not. A palindrome is a word, phrase, or sequence that reads the same forwards as it does backward.
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).
function isPalindrome(str) {
// Remove non-alphanumeric characters and convert to lowercase
const cleanedStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
// Compare the cleaned string with its reverse
return cleanedStr === cleanedStr.split('').reverse().join('');
}
// Test cases
console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false
console.log(isPalindrome("A man, a plan, a canal, Panama")); // true
This code defines a function isPalindrome
that takes a string as an input, cleans the string by removing non-alphanumeric characters and converting it to lowercase, and then checks whether the cleaned string is the same as its reverse.
In the example test cases provided, the program should return true
for the first and third test cases, and false
for the second one.
Demo
Palindrome Checker
Now write the same program in your programming language in comments.
Comments
Post a Comment