Palindrome Checker using Stack in C++

Palindrome Checker using Stack in C++ (With Source Code)

Palindrome Checker using Stack in C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn  to create a Palindrome checker using Stack in C++ (With Source Code).

Palindrome Checker using Stack in C++

A Palindrom checker implented using C++ is a console based program which allows the user to enter a string and uses a stack to detect if it is a palindrome. It eliminates punctuation and symbols, lowercases the string, displays the modified string, reverses the changed string, and lastly decides whether or not the modified string is a palindrome.

Player Management System using C++ (With Source Code)

Features of the Palindrome System:

  • The system is user-friendly as it asks the user to enter a string.
  • System handles strings of up to MAX characters and performs appropriate length checks.
  • It can manipulate string, removes punctuation and symbols from the input string.
  • It converts the string to lowercase before checking for a palindrome.
  • System uses the Stack implementation to check for palindromes.
  • System displays the reverse of the modified string for comparison.
  • System can determine if the modified string is a palindrome and provides appropriate output.

Overview of the UI:

Enter a String:
  • The user will enter a string then,
String extracted after removing punctuations and symbols
String converted to lower case
Reverse of entered string:
  • If entered string is palindrome, the system will display:
Entered string is a Palindrome.
  • If entered string is not palindrome:
Entered string is not a Palindrome.

Palindrome Checker using Stack in C++ Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

// PALINDROME USING STACK.........
#include<iostream>
#include<string.h>
using namespace std;
#define MAX 50
class Stack
{
private:
  char data[MAX],str[MAX];
  int top,length,count;
  void pushData(char);
  char popData();
public:
  Stack()
  {
    top=-1;
    length=0;
    count=0;
  }
  void getString();
  void checkPalindrome();
  void extractString();
  void displayReverse();
};
int main()
{
  system("color 5a");
  Stack obj;
  obj.getString();
  obj.extractString();
  cout<<"\n String extracted after removing punctuations and symbols";
  cout<<"\n String converted to lower case";
  cout<<"\n Reverse of entered string: ";
  obj.displayReverse();
  obj.checkPalindrome();
  return 0;
}
void Stack::getString()
{
  cout<<"\n Enter a String: ";
  cin.getline(str,MAX);
  length=strlen(str);
}
void Stack::extractString()
{
  char temp[MAX];
  int i,j;
  for(i=0; i<length; i++)
  {
    temp[i]=str[i];
  }
  j=0;
  for(i=0; i<length; i++ ) 
  {
    if(isalpha(temp[i]))
    {
      str[j]=tolower(temp[i]);
      j++;
    }
  }
  length=j;   //update length with new str length
}
void Stack::checkPalindrome()
{
  for(int i=0; i<length; i++)
    pushData(str[i]);

  for(int i=0; i<length; i++)
  {
    if(str[i]==popData())
      count++;
  }
  if(count==length)
  {
    cout<<"\n Entered string is a Palindrome. \n";
  }
  else  cout<<"\n Entered string is not a Palindrome. \n";
}
void Stack::displayReverse()
{
  for(int i=length-1; i>=0; i--)
    cout<<str[i];
}
void Stack::pushData(char temp)
{
  if(top==MAX-1)
  {
    cout<<"\n Stack Overflow!!!";
    return;
  }
  top++;
  data[top]=temp;
}
char Stack::popData()
{
  if(top==-1)
  {
    cout<<"\n Stack Underflow!!!";
    char ch='\n';
    return ch;
  }
  char temp=data[top];
  top--;
  return temp;
  return 0;
}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream  (input and output stream), string.h (string manipulation).
  • We will create a class called Stack with private and public member functions and variables. The private members are – data[MAX] array to store characters in the stack, str[MAX] array to store the input string.  top (An integer indicating the top position of the stack.),length (integer representing the length of the input string.), count (integer to keep track of matching characters while checking for a palindrome), pushData(char) function to push a character onto the stack, popData() function to pop a character from the stack and the public members are – Stack(), getString(), checkPalindrome(), extractString(), displayReverse();
  • The main() function will define the console color by system(“color 5a”). Then we will create an object called ‘obj’ for the class Stack. We will call the getString() function to get the input string from the user, and extractString() function to remove punctuation, symbols, and convert the string to lowercase for the obj object of class Stack. Then we will print some statements regarding the modifications. The function will be called – displayReverse() to display the reverse of the modified string, and obj.checkPalindrome() to o check if the modified string is a palindrome.
  • Now we will define the functions outside the class, as we have already made their forward declarations.
  • The getString() function will get the input of the string from the user using the cin.getline().
  • The extractString() function will remove punctuation, symbols, and convert the string to lowercase. We will declare variables – temp, i, and j and use for loops for the modifications
  • The checkPalindrom() function will finally determine if the modified string is a palindrome or not. We will push the string in the stack and pop it using the for loops. If the value of count is same as length then the entered string will be palindrome else not.
  • The displayReverse() function will display the string reversely.
  • The pushData() function will push the characters into the stack. We will also create an if condition for the stack overflow.
  • The popData() function will pop a character from the stack. We will also create an if condition for the stack underflow.
  • This sums up our project of palindrome using C++.

Patient Management System using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Palindrome Checker using Stack in C++

Video Output:

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Simple RPG Monster Game using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodewithRandom



Leave a Reply