A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.
user_string = input()
low = 0
high = len(user_string)-1
result = True
while low < high:
if (user_string[low] == ' '):
low += 1
elif (user_string[low]!=user_string[high]):
result = False
break
low += 1
high -= 1
if result:
print(user_string, 'is a palindrome')
else:
print(user_string, 'is not a palindrome')
could someone please explain to me line by line of this program!!. I don't understand high and low... Is there another variable that makes sense?
the language is Python
CODE :
# we take input from user
user_string = input()
# low is used for the representing starting index
low = 0
# high is used for the last index .
high = len(user_string)-1
result = True
while low < high:
# if starting index of string is space we will
increase the index value
if (user_string[low] == ' '):
low += 1
#if starting index string is not equal to last index of string
then print false. Else print the string is palindrome.
elif (user_string[low]!=user_string[high]):
result = False
break
low += 1
high -= 1
if result:
print(user_string, 'is a palindrome')
else:
print(user_string, 'is not a palindrome')
NOTE : PLEASE GIVE ME UP VOTE .THANK YOU.
Get Answers For Free
Most questions answered within 1 hours.