Write a programme to check whether given input is palindrome or notConstraintsABC != PalindromeMAM == Palindrome123 != Palindrome151 == Palindrome
Question
Write a programme to check whether given input is palindrome or not
Constraints
ABC != Palindrome
MAM == Palindrome
123 != Palindrome
151 == Palindrome
Solution
Step 1: Break Down the Problem
- We need a function to check if a given input string is a palindrome.
- A palindrome reads the same forwards and backwards.
- We should consider case sensitivity and ignore spaces and special characters.
Step 2: Relevant Concepts
- A string is a palindrome if the reversed string is equal to the original string.
- We can use slicing to reverse the string in Python.
Step 3: Analysis and Detail
- Define a function
is_palindrome
that takes a string as input. - Normalize the string by converting it to lower case and removing non-alphanumeric characters.
- Reverse the string and compare it to the original normalized string.
Step 4: Verify and Summarize
- We can test the function with provided constraints:
- "ABC" should return
False
. - "MAM" should return
True
. - "123" should return
False
. - "151" should return
True
.
- "ABC" should return
Final Answer
Here is the Python code to perform the palindrome check:
def is_palindrome(s):
# Normalize the string: convert to lower case and remove non-alphanumeric characters
normalized = ''.join(char.lower() for char in s if char.isalnum())
# Check if the normalized string is the same as its reverse
return normalized == normalized[::-1]
# Test the function with the provided inputs
print(is_palindrome("ABC")) # Output: False
print(is_palindrome("MAM")) # Output: True
print(is_palindrome("123")) # Output: False
print(is_palindrome("151")) # Output: True
This program effectively checks for palindromes according to the specified constraints.
Similar Questions
Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.
Write a C++ program to check if a given number is a palindrome. A palindrome is a number that remains the same when its digits are reversed.
Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.
Type the program's outputa = int(input())b = int(input())c = int(input())while a < b: print(a) if a > c: break a += 2 Input2144Output
Write a program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.