You are given a string, write a program to find whether the string is palindrome or not.
Note: Treat uppercase letters and lowercase letters as same when comparing letters. Ignore spaces and quotes within the string.
In the given example, the string No lemon no melon is a palindrome as we are ignoring spaces. So, the output should be True.
God's Dog and the output should be True
Was it a cat I saw? so the output should be False
def Palindrome(sentence):
x, y= 0, len(sentence) - 1
sentence = sentence.lower()
while (x <= y):
if (not(sentence[x] >= 'a' and sentence[x] <= 'z')):
x += 1
elif (not(sentence[y] >= 'a' and sentence[y] <= 'z')):
y -= 1
elif (sentence[x] == sentence[y]):
x += 1
y -= 1
else:
return False
return True
s = "No lemon no melon"
if (Palindrome(s)):
print ("True")
else:
print ("False")
Comments
Leave a comment