Question

Can anyone help me fix this program to make sure use stack and use STL library...

Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome.

Thank you

stackPalindrome.h

#ifndef_STACK_PALINDROME_H
#define_STACK_PALINDROME_H
template <class StackPalindrome>
class StackPalindrome
{
public:
   virtual bool isEmpty() const = 0;
   virtual bool push(const ItemType& newEntry) = 0;
   virtual bool pop() = 0;
   virtual ItemType peek() const = 0;
};
#endif

stack.cpp

#include<iostream>
#include<string>
#include<new>
#include "stackPalindrome.h"
using namespace std;

template <class ItemType>
class OurStack
{
private:
   stack OurStack;
public:
   bool OurStack::isEmpty()
   {
       return Ourstack.empty();
   }
};
bool OurStack::push(const ItemType& newEntry)
{
   Ourstack.push(newEntry);

   if (Ourstack.top() == newEntry)
       return true;
   else
       return false;
}
bool OurStack::pop()
{
   if (!Ourstack.empty())
   {
       Ourstack.pop();
       return true;
   }
   else
       return false;
}
ItemType OurStack::peek()
{
   return Ourstack.top();
}
bool isPalindrome(string inp_string)
{
   OurStack char_stack;
   bool chckStatus = true;
   int string_len = (int)inp_string.length();
   if (string_len > 0)
   {
       for (int i = 0; i < string_len; i++)
           char_stack.push(inp_string[i]);
       int i = 0;
       while (chckStatus && !char_stack.isEmpty() && (i < string_len))
       {
           char item_stack = char_stack.peek();
           char_stack.pop();
           if (item_stack != inp_string[i])
               chckStatus = false;
           else
               i++;
       }
   }
   else
       chckStatus = false;
   return chckStatus;
}
int main()
{
   string name1 = "love to hoot";
   string name2 = "Testing Palindrome ";
   if (isPalindrome(name1))
       cout << " The string " << name1 << "is palindrome.\n"
   else
       cout << " The string " << name1 << "is not palindrome.\n"
       if (isPalindrome(name2))
           cout << " The string " << name2 << "is palindrome.\n"
       else
           cout << " The string " << name2 << "is not palindrome.\n"
           system("pause");
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the C++ code Note that I have done proper indentation but this code is automatically left alligned on this interface

StackPalindrome.h -

#ifndef STACK_PALINDROME_H
#define STACK_PALINDROME_H
#include<bits/stdc++.h>
using namespace std;
template <class ItemType>
class StackPalindrome
{
private:
stack <ItemType>Ourstack;
public:
bool isEmpty();
bool push(const ItemType& newEntry) ;
bool pop() ;
ItemType peek() ;
};
#endif

stack.cpp -

#include<bits/stdc++.h>
#include "stackPalindrome.h"
using namespace std;
template <class ItemType>
bool StackPalindrome<ItemType>::isEmpty()
{
return Ourstack.empty();
}
template <class ItemType>
bool StackPalindrome<ItemType>::push(const ItemType& newEntry)
{
Ourstack.push(newEntry);
if (Ourstack.top() == newEntry)
return true;
else
return false;
}
template <class ItemType>
bool StackPalindrome<ItemType>::pop()
{
if (!Ourstack.empty())
{
Ourstack.pop();
return true;
}
else
return false;
}
template <class ItemType>
ItemType StackPalindrome<ItemType>::peek()
{
return Ourstack.top();
}
bool isPalindrome(string inp_string)
{
StackPalindrome <char>char_stack;
bool chckStatus = true;
int string_len = (int)inp_string.length();
if (string_len > 0)
{
for (int i = 0; i < string_len; i++)
char_stack.push(inp_string[i]);
int i = 0;
while (chckStatus && !char_stack.isEmpty() && (i < string_len))
{
char item_stack = char_stack.peek();
char_stack.pop();
if (item_stack != inp_string[i])
chckStatus = false;
else
i++;
}
}
else
chckStatus = false;
return chckStatus;
}
int main()
{
string name1 = "love to hoot";
string name2 = "Testing Palindrome ";
if (isPalindrome(name1))
cout << " The string " << name1 << "is palindrome.\n";
else
cout << " The string " << name1 << "is not palindrome.\n";
if (isPalindrome(name2))
cout << " The string " << name2 << "is palindrome.\n";
else
cout << " The string " << name2 << "is not palindrome.\n";
system("pause");
return 0;
}

Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Add a comment
Know the answer?
Add Answer to:
Can anyone help me fix this program to make sure use stack and use STL library...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const...

    stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT