Coders Packet

Program to check if a given string is a Palindrome using C++

By Shravya Chinta

A packet to check if a given string is a Palindrome using C++. This program is implemented using 3 methods i.e., reversed string method, using a Stack by implementation, and as a data structure.

A string or a number is said to be a Palindrome if it reads the same forwards and backward. For example, the word 'madam' is a palindrome since the reversed string(madam), is the same as the original string. 

Some examples of palindromes: 121, 234565432, kayak, level, etc

1. Reversed string method:

In this method, we declare an empty string that stores the original string in reverse by iterating it from the end with a 'for' loop. If both the strings are equal, then the given string is a Palindrome.

2. Using Stack Data Structure:

Declare a Stack and push all the characters of the string using a 'for' loop. Now, pop all the characters in the Stack using a 'for' loop and compare to see if the reversed string is the same as the original string. Since Stack is a 'last-in, first-out(LIFO)' data structure, if we find a mismatch, the input string is not a palindrome string. Otherwise, it is a palindrome.

3. Stack method by implementing Stack:

In this method, instead of using a Stack data structure, we implement Stack and perform the same algorithm as the above method.

Download Complete Code

Comments

No comments yet