Answer to Question #220300 in Python for chandi

Question #220300

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




1
Expert's answer
2021-07-25T08:38:15-0400
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")
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS