You are going to write a program that checks to see if a string is a palindrome.
ID: 3595946 • Letter: Y
Question
You are going to write a program that checks to see if a string is a palindrome.
“A palindrome is a word, phrase, number, or other sequence of characters which
reads the same backward as forward, such as madam or race car. Sentence-
length palindromes may be written when allowances are made for adjustments of
capital letters, punctuation, or word dividers, such as “A man, a plan, a canal
Panama!”. “ (Wikipedia) You will use a stack and a queue to determine if a
phrase is a palindrome. You will ignore spaces and punctuation. You may use
either a c-string or a c++ string. (Hint: You should store characters on the stack
and queue to make this easy.)
The stack class is written for you, but will need to be modified to use the correct
type. You will need to write the queue class. The Node class will need to be
modified to work with the stack and queue classes.
The code to check to see if the string is a palindrome should be in the main
function. You should have 2 test cases that you test in main – one that is
palindrome and one that isn’t. Then you should ask the user for a phrase until
the user wishes to terminate.
Specific information for classes:
Stack class data
top – points to first Node in list
count – keeps track of number of Nodes in list
Stack class functions
Any needed constructors
push – adds a Node to the top of the stack
pop – Removes a Node from the top of the stack
stack_top – Returns the item at the top of the stack without affecting the
stack
empty – returns true if stack is empty, false otherwise
Queue class data
front – points to first Node in list
rear – points to the last Node in list
count – keeps track of number of Nodes in list
Stack class functions
Any needed constructors
enqueue – adds a Node to the end of the list
dequeue – Removes a Node from the front of the queue
get_front – Returns the item at the front of the queue without affecting the
queue
empty – returns true if queue is empty, false otherwise
Note: Remember that a stack is created in static (automatic memory) and a
Node is in dynamic memory.
In the main program, you will test 2 cases; one that is a palindrome and one that
isn’t. Then you will ask the user for a phrase and indicate whether or not the
phrase is a palindrome. You will loop until the user wishes to exit.
Sample:
The phrase “race car” is a palindrome.
The phrase “man made” is not a palindrome.
Please enter a phrase:
Madam
Madam is a palindrome.
Explanation / Answer
I made solution for stac using implement for given string is palindrom or not.
See the below code:
#include<iostream>
#include<string.h>
#include<ctype.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() {
Stack obj;
obj.getString();
obj.extractString();
cout<<" String extracted after removing punctuations and symbols";
cout<<" String converted to lower case";
cout<<" Reverse of entered string: ";
obj.displayReverse();
obj.checkPalindrome();
return 0;
}
void Stack::getString() {
cout<<" 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<<" Entered string is a Palindrome. ";
}
else cout<<" Entered string is not a Palindrome. ";
}
void Stack::displayReverse() {
for(int i=length-1; i>=0; i--)
cout<<str[i];
}
void Stack::pushData(char temp) {
if(top==MAX-1) {
cout<<" Stack Overflow!!!";
return;
}
top++;
data[top]=temp;
}
char Stack::popData() {
if(top==-1) {
cout<<" Stack Underflow!!!";
char ch=' ';
return ch;
}
char temp=data[top];
top--;
return temp;
}
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main (void)
{
queue <char> q;
string letters;
int length;
cout<<"Please enter a series of characters."<<endl;
getline (cin, letters);
bool isPalindrome = false;
if (letters.size() > 0)
{
int length = letters.size() / 2;
for (int i=0; i<length; i++)
{
q.push(letters[i]);
}
isPalindrome = true;
for (int i = 1; i <= length && isPalindrome; ++i)
{
isPalindrome = q.front() == letters[letters.size() - i];
q.pop();
}
}
if(!isPalindrome)
{
cout<<"Is not a palindrome."<<endl;
}
else
{
cout<<"Is a palindrome."<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.