Stack Assignment: Testing if a word is a palindrome.

Palindromes are words that are spelt the same way backward as they are forward.

Task

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 1Please enter a string

ken

ken is not a palindrome.
Example 2Please enter a string

deified

defied is a palindrome.
sample runs

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.

Requirements

  1. 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.
  2. 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. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s