Palindromes are words that are spelt the same way backward as they are forward.
Create a program which accepts a string of text from the user and outputs whether or not the string is a palindrome. You may assume that the user always enters a single word for input, i.e no spaces are entered.
below are two examples of running the program. (User input is bolded)
|Example 1||Please enter a string|
ken is not a palindrome.
|Example 2||Please enter a string|
defied is a palindrome.
You may use the code found in this post Stacks Implementations and Variations. A suitable example of a stack can be found in the zipped folder 05 U2 M1 S2 Stack-String ReversalErrorChecks.
- Create a stack which uses the datatype char and is managed by a variable top which stores the index of the top of the stack.
- Use the stack to determine if an input string is a palindrome by first pushing all the characters of the string onto the stack, and then popping from the stack one at a time comparing each popped character to each character in the original string.
That is, compare your first popped character to the 1st character of the string, your 2nd popped character to the 2nd character of the string….and so on until all characters are compared. If any comparison fails, then the string is not a palindrome. If all passes , then the word is a palindrome.
© 2020 Vedesh Kungebeharry. All rights reserved.