Monthly Archives: April 2021
C++ || Simple Palindrome Checker Using A Stack & Queue Using C++
The following is a program which demonstrates how to use a stack and a queue to test for a palindrome using C++.
The program demonstrated on this page is an updated version of a previous program of the same type. This program is great practice for understanding how the two data structures work.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
Template Classes - What Are They?
Stacks
Queues
LIFO - Last In First Out
FIFO - First In First Out
1. Overview
This program first asks the user to enter in text which they wish to compare for similarity. The data is then saved into the system using the “enqueue” and “push” functions available within the queue and stack classes. After the data is obtained, a while loop is used to iterate through both classes, checking to see if the characters at each location within both classes are the same. If the text within both classes are the same, it is a palindrome.
2. Palindrome Checker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jul 22, 2012 // Updated: Apr 2, 2021 // Taken From: http://programmingnotes.org/ // File: palindrome.cpp // Description: Demonstrates a palindrome checker using a stack & queue // ============================================================================ #include <iostream> #include <cctype> #include <string> #include <queue> #include <stack> int main() { // Declare variables std::string text = ""; bool isPalindrome = true; std::queue<char> queue; std::stack<char> stack; // Get data from user std::cout << "Enter in some text to see if its a palindrome: "; std::getline(std::cin, text); // Place the characters into the queue and stack for storage for (auto ch : text) { ch = std::toupper(ch); queue.push(ch); stack.push(ch); } // Determine if the string is a palindrome while (!queue.empty() && !stack.empty()) { if (queue.front() != stack.top()) { isPalindrome = false; break; } queue.pop(); stack.pop(); } // Display results to the screen std::cout << "\n\n" << text; if (isPalindrome) { std::cout << " is a palindrome!\n"; } else { std::cout << " is NOT a palindrome..\n"; } std::cin.get(); return 0; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output:
====== RUN 1 ======
Enter in some text to see if its a palindrome: StEP on No pETS
StEP on No pETS is a palindrome!
====== RUN 2 ======
Enter in some text to see if its a palindrome: Hello World
Hello World is NOT a palindrome..
====== RUN 3 ======
Enter in some text to see if its a palindrome: Kenneth, Jennifer, Lynn, Sole
Kenneth, Jennifer, Lynn, Sole is NOT a palindrome..