I need help with this C++ Data Structures program. Here are the instructions: He
ID: 3860916 • Letter: I
Question
I need help with this C++ Data Structures program. Here are the instructions:
Here is "stack.h":
Please use Stacks (first in last out) principle to create this program, by using the push and pop operations. Thanks for your assistance!
Filename: palindrome epp Filename: palindrome. cpp Write a program that uses a stack object to determine if a string is a palindrome (i.e. the string is spelled identically backward and forward). The program should ignore spaces and punctuation. Go ahead and start your program by reading in a C-style string from standard input, using the getline function. You may assume a limit of 100 characters on the string (although this can be written, with a little more effort, to accept any size string). Your algorithm must make use of a stack (of type char). Use "stack.h" (you don't need to change this file). Ignore spacing and punctuation, as well as special characters and digits (i.e. only count letters as part of a palindrome, and account for upper/lower case. For example, 'B' and 'b' are matching letters). Sample runs: (user input underlined) Please enter a string: ABCDEFGHGFEDCBA ABCDEFGHGFEDCBA" IS a palindrome Please enter a string: ic .xxvaz" , The quick brown fox" is NOT palindrome Please enter a string: Cig ic. "Cigar? Toss it in a can. It is so tragic." IS a palindrome Since you are reading data into a C-style string to begin, you may use any of the libraries iostream>, , and , if you like. (The first, of course, for I/O, and the other two, since they deal with C-style strings and characters)Explanation / Answer
#include "Stack.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int count = 0;
int len;
string w;
char a, c;
cout << "Please enter a word ";
getline(cin, w);
len = w.len() + 1;
char * arr = new char[len];
strcpy(arr, w.c_str());
Stack palindrome(len);
while(count <= (len - 1))
{
c = arr[count];
palindrome.push(c);
count++;
}
count = 0;
while(count <= (len - 1))
{
palindrome.pop(a);
if(a != arr[count])
{
cout << "The string is not a palindrome ";
return 0 ;
}
count++;
}
cout << "The string is a palindrome ";
delete [ ] arr;
return 0;
}
}
#include "Stack.h"
#include <string>
Stack::Stack(int len)
{
stackArray = new char[len];
stackSize = len;
top = -1;
}
Stack::~Stack()
{
delete [] stackArray;
}
void Stack::push(char str)
{
top++;
stackArray[top] = str;
}
void Stack::pop(char &str)
{
if(isEmpty())
{
cout <<"No string ";
}
else
{
str = stackArray[top];
top--;
}
}
bool Stack::isEmpty() const
{
bool status;
if(top == -1)
status = true;
else
status = false;
return status;
}