A palindrome is a word that can be read both ways, like 'kayak'.
This example will show how to find if a given word is a palindrome using Python3.
First, we need to turn a string into a stack (we will use arrays as stacks).
str = "string"
stk = [c for c in str] #this makes an array: ["s", "t", "r", "i", "n", "g"]
stk.append("s") #adds a letter to the array
stk.pop() #pops the last element
Now, we have to invert the word.
def invert(str):
stk = [c for c in str]
stk2 = []
while len(stk) > 0:
stk2.append(stk.pop())
#now, let's turn stk2 into a string
str2 = ""
for c in stk2:
str2 += c
return str2
Now we can compare the word and the inverted form.
def palindrome(str):
return str == invert(str)