Your task today is to create a program that checks if a given word or phrase is a palindrome. A palindrome is a sequence of characters that reads the same backward as forward, ignoring spaces, punctuation, and capitalization.
For example:
"racecar"
"True"
"A man a plan a canal Panama"
"True"
This challenge will help you work with strings, loops, and data cleaning techniques.
This challenge teaches you key programming concepts:
There are two main approaches to solving this problem:
# Get input from the user
text = input("Enter a word or phrase: ")
# Clean the string: remove spaces, punctuation, and make it lowercase
cleaned_text = "".join(char.lower() for char in text if char.isalnum())
# Check if it's a palindrome using loops
is_palindrome = True
for i in range(len(cleaned_text) // 2):
if cleaned_text[i] != cleaned_text[-(i + 1)]:
is_palindrome = False
break
# Output the result
print("Is it a palindrome?", is_palindrome)
# Get input from the user
text = input("Enter a word or phrase: ")
# Clean the string: remove spaces, punctuation, and make it lowercase
cleaned_text = "".join(char.lower() for char in text if char.isalnum())
# Check if the cleaned string is equal to its reverse
is_palindrome = cleaned_text == cleaned_text[::-1]
# Output the result
print("Is it a palindrome?", is_palindrome)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter a word or phrase: ");
String text = scanner.nextLine();
// Clean the string: remove spaces, punctuation, and make it lowercase
String cleanedText = text.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Check if it's a palindrome using loops
boolean isPalindrome = true;
for (int i = 0; i < cleanedText.length() / 2; i++) {
if (cleanedText.charAt(i) != cleanedText.charAt(cleanedText.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
// Output the result
System.out.println("Is it a palindrome? " + isPalindrome);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter a word or phrase: ");
String text = scanner.nextLine();
// Clean the string: remove spaces, punctuation, and make it lowercase
String cleanedText = text.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Check if the cleaned string is equal to its reverse
String reversedText = new StringBuilder(cleanedText).reverse().toString();
boolean isPalindrome = cleanedText.equals(reversedText);
// Output the result
System.out.println("Is it a palindrome? " + isPalindrome);
}
}
// Get input from the user
let text = prompt("Enter a word or phrase:");
// Clean the string: remove spaces, punctuation, and make it lowercase
let cleanedText = text.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
// Check if it's a palindrome using loops
let isPalindrome = true;
for (let i = 0; i < cleanedText.length / 2; i++) {
if (cleanedText[i] !== cleanedText[cleanedText.length - 1 - i]) {
isPalindrome = false;
break;
}
}
console.log("Is it a palindrome?", isPalindrome);
// Get input from the user
let text = prompt("Enter a word or phrase:");
// Clean the string: remove spaces, punctuation, and make it lowercase
let cleanedText = text.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
// Check if the cleaned string is equal to its reverse
let isPalindrome = cleanedText === cleanedText.split("").reverse().join("");
console.log("Is it a palindrome?", isPalindrome);
In Day 16: Sorting a List, you’ll write a program that sorts a list of numbers in ascending or descending order.