68.3k views
3 votes
Write a program that uses a stack to test input strings to determine whether they are palindromes. A palindrome is a sequence of characters that reads the same as the sequence in reverse; for example, noon.

User Htho
by
4.5k points

1 Answer

4 votes

Answer:

Here the code is given as follows,

Step-by-step explanation:

def isPalindrome(x):

stack = []

#for strings with even length

if len(x)%2==0:

for i in range(0,len(x)):

if i<int(len(x)/2):

stack.append(x[i])

elif stack.pop()!=x[i]:

return False

if len(stack)>0:

return false

return True

#for strings with odd length

else:

for i in range(0,len(x)):

if i==int(len(x)/2):

continue

elif i<int(len(x)/2):

stack.append(x[i])

elif stack.pop()!=x[i]:

return False

if len(stack)>0:

return false

return True

def main():

while True:

string = input("Enter a string or Return to quit: ")

if string == "":

break

elif isPalindrome(string):

print("It's a palindrome")

else:

print("It's not a palindrome")

if __name__ == '__main__':

main()

Write a program that uses a stack to test input strings to determine whether they-example-1
Write a program that uses a stack to test input strings to determine whether they-example-2
User Knightofni
by
4.1k points