Question

A palindrome is a word or a phrase that is the same when read both forward...

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  

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A palindrome is a word or a phrase that is the same when read both forward...
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. Ex: If the input is: bob the output is: bob is a palindrome Ex: If the input is: bobby the output is: bobby is not a palindrome IN C++ PLEASE!
1. A Palindrome is a word that is the same backwards as it is forwards. For...
1. A Palindrome is a word that is the same backwards as it is forwards. For example, the words kayak and madam are Palindromes. a. Request an 5 character value from the console. b. If the input string is not 5 characters, print that the word is not a 5 character word and exit. c. If the input string is 5 characters, determine if the word is or is not a Palindrome. Hint: String indexes can be used to compare...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT