Question

In: Computer Science

C++: A palindrome is a word, phrase, number, or other sequence of characters which reads the...

C++:

  1. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".

Write a program using Stacks-Array and Queue-Array data structure we did in class to check whether a given string is a palindrome. (50 points)

      This is an exercise in Stacks and Queues. So you need to use both datastructures to test :

In the main you can test whether a string is a palindrome using the method

bool palindromeTest( string test) and should include “QueArr.h” and “StackArr.h”

QueArr.h

#include <iostream>

using namespace std;

template <class ItemType>
class QueArr
{
private:
   int maxSize;
   int front;
   int rear;
   ItemType * items;

public:
   QueArr();
   QueArr(int size);
   ~QueArr();

   void makeEmpty();
   bool isEmpty() const;
   bool isFull() const;

   void add(ItemType item);
   void remove(ItemType& item);
   void print() const;


};

template <class ItemType>
QueArr<ItemType>::QueArr()
{
   maxSize = 100;
   front = maxSize - 1;
   rear = maxSize - 1;
   items = new ItemType [maxSize];
}

template <class ItemType>
QueArr<ItemType>::QueArr( int size )
{
   maxSize = size;
   front = maxSize - 1;
   rear = maxSize - 1;
   items = new ItemType[maxSize];
}

template <class ItemType>
QueArr<ItemType>::~QueArr()
{
   delete[] items;
}

template <class ItemType>
void QueArr<ItemType>::makeEmpty()
{
   front = maxSize - 1;
   rear = maxSize - 1;
}

template <class ItemType>
bool QueArr<ItemType>::isEmpty() const
{
   return (rear == front);
}

template <class ItemType>
bool QueArr<ItemType>::isFull() const
{
   return ((rear + 1) % maxSize == front);
}

template <class ItemType>
void QueArr<ItemType>::add(ItemType item)
{
   if (isFull())
       cout << "Queue is Full" << endl;
   else
   {
       rear = (rear + 1) % maxSize;
       items[rear] = item;
   }
}

template <class ItemType>
void QueArr<ItemType>::remove(ItemType& item)
{
   if (isEmpty())
       cout << "Queue is empty" << endl;
   else
   {
       front = (front + 1) % maxSize;
       item = items[front];
   }
}

template <class ItemType>
void QueArr<ItemType>::print() const
{
   if (isEmpty())
       cout << "Que is empty" << endl;
   else
   {
       //cout << front << ", " << length << endl;
       int temp = front;
       while(temp != rear)
       {
           temp = (temp + 1) % maxSize;
           cout << items[temp] << " ";
       }
       cout << endl;
   }
}


StackArr.h

#include <iostream>

using namespace std;

template <class ItemType>
class StackArr
{
public:
   StackArr();           //Empty constructor
   StackArr(int max); //Constructor which takes a size

   bool IsEmpty() const;
   bool IsFull() const;

   void Push(ItemType item);
   void Pop();

   ItemType Top() const;
   void PrintStack();

private:
   int top;
   int maxStack;
   ItemType* items;
   int length;

};

template <class ItemType>
StackArr<ItemType>::StackArr()
{
   maxStack = 100;
   top = -1;
   items = new ItemType[maxStack];
   length = 0;
}

template <class ItemType>
StackArr<ItemType>::StackArr( int max)
{
   maxStack = max;
   top = -1;
   items = new ItemType[maxStack];
   length = 0;
}

template <class ItemType>
bool StackArr<ItemType>::IsEmpty() const
{
   return (top == -1);
}

template <class ItemType>
bool StackArr<ItemType>::IsFull() const
{
   return (top == maxStack - 1);
}

template <class ItemType>
void StackArr<ItemType>::Push(ItemType item)
{
   if (IsFull())
   {
       cout << "The stack is full and item cannot be pushed";
   }
   else
   {
       top++;
       items[top] = item;
       length++;
   }
}

template <class ItemType>
void StackArr<ItemType>::Pop()
{
   if(IsEmpty())
   {
       cout << "The stack is empty and item cannot be popped";
   }
   else
   {
       top--;
       length--;
   }

}

template <class ItemType>
ItemType StackArr<ItemType>::Top() const
{
   if (IsEmpty())
   {
       cout << "The stack is empty and no item on top";
   }
   else
       return items[top];

}

template <class ItemType>
void StackArr<ItemType>::PrintStack()
{
   if (length == 0)
       cout << "Stack is empty" << endl;
   else
   {
       for (int i = 0; i < length; i++)
           cout << items[i] << ", ";
       cout << endl;
   }

}


Solutions

Expert Solution

Note : There were a few mistakes in your Queue Class. I’ve fixed them and posted all the code below including main.cpp which checks for palindrome.

Code:

//QueArr.h:

#include <iostream>

using namespace std;

template <class ItemType>

class QueArr

{

private:

   int maxSize;

   int front;

   int rear;

   ItemType * items;

public:

   QueArr();

   QueArr(int size);

   ~QueArr();

   void makeEmpty();

   bool isEmpty() const;

   bool isFull() const;

   void add(ItemType item);

   void remove();

   ItemType Front() const;

   void print() const;

};

template <class ItemType>

QueArr<ItemType>::QueArr()

{

   maxSize = 100;

   front = -1;

   rear = -1;

   items = new ItemType [maxSize];

}

template <class ItemType>

QueArr<ItemType>::QueArr( int size )

{

   maxSize = size;

   front = maxSize - 1;

   rear = maxSize - 1;

   items = new ItemType[maxSize];

}

template <class ItemType>

QueArr<ItemType>::~QueArr()

{

   delete[] items;

}

template <class ItemType>

void QueArr<ItemType>::makeEmpty()

{

   front = maxSize - 1;

   rear = maxSize - 1;

}

template <class ItemType>

bool QueArr<ItemType>::isEmpty() const

{

   return (rear == -1);

}

template <class ItemType>

bool QueArr<ItemType>::isFull() const

{

   return ((rear + 1) % maxSize == front);

}

template <class ItemType>

void QueArr<ItemType>::add(ItemType item)

{

   if (isFull())

       cout << "Queue is Full" << endl;

   else if(rear == -1)

   {

     front = 0;

     rear = 0;

     items[rear] = item;

   }

   else

   {

       rear = (rear + 1) % maxSize;

       items[rear] = item;

   }

}

template <class ItemType>

void QueArr<ItemType>::remove()

{

   if (isEmpty())

       cout << "Queue is empty" << endl;

   else

   {

       front = (front + 1) % maxSize;

   }

}

template <class ItemType>

ItemType QueArr<ItemType>::Front() const

{

   if (isEmpty())

   {

       cout << "The stack is empty and no item on top";

   }

   else

       return items[front];

}

template <class ItemType>

void QueArr<ItemType>::print() const

{

   if (isEmpty())

       cout << "Que is empty" << endl;

   else

   {

       //cout << front << ", " << length << endl;

       int temp = front;

       while(temp != rear)

       {

           temp = (temp + 1) % maxSize;

           cout << items[temp] << " ";

       }

       cout << endl;

   }

}

//StackArr.h:

#include <iostream>

using namespace std;

template <class ItemType>

class StackArr

{

public:

   StackArr();           //Empty constructor

   StackArr(int max); //Constructor which takes a size

   bool IsEmpty() const;

   bool IsFull() const;

   void Push(ItemType item);

   void Pop();

   ItemType Top() const;

   void PrintStack();

private:

   int top;

   int maxStack;

   ItemType* items;

   int length;

};

template <class ItemType>

StackArr<ItemType>::StackArr()

{

   maxStack = 100;

   top = -1;

   items = new ItemType[maxStack];

   length = 0;

}

template <class ItemType>

StackArr<ItemType>::StackArr( int max)

{

   maxStack = max;

   top = -1;

   items = new ItemType[maxStack];

   length = 0;

}

template <class ItemType>

bool StackArr<ItemType>::IsEmpty() const

{

   return (top == -1);

}

template <class ItemType>

bool StackArr<ItemType>::IsFull() const

{

   return (top == maxStack - 1);

}

template <class ItemType>

void StackArr<ItemType>::Push(ItemType item)

{

   if (IsFull())

   {

       cout << "The stack is full and item cannot be pushed";

   }

   else

   {

       top++;

       items[top] = item;

       length++;

   }

}

template <class ItemType>

void StackArr<ItemType>::Pop()

{

   if(IsEmpty())

   {

       cout << "The stack is empty and item cannot be popped";

   }

   else

   {

       top--;

       length--;

   }

}

template <class ItemType>

ItemType StackArr<ItemType>::Top() const

{

   if (IsEmpty())

   {

       cout << "The stack is empty and no item on top";

   }

   else

       return items[top];

}

template <class ItemType>

void StackArr<ItemType>::PrintStack()

{

   if (length == 0)

       cout << "Stack is empty" << endl;

   else

   {

       for (int i = 0; i < length; i++)

           cout << items[i] << ", ";

       cout << endl;

   }

}

//main.cpp:

#include<iostream>

#include<string>

#include "QueArr.h"

#include "StackArr.h"

using namespace std;

bool palindromeTest(string testStr)

{

StackArr <char> stk;

QueArr <char> que;

int loop;

for (loop = 0; loop < testStr.length(); loop++) {

    que.add(testStr[loop]);

    stk.Push(testStr[loop]);

}

bool isPalindrome = true;

while ((!que.isEmpty()) && (!stk.IsEmpty()) && isPalindrome) {

    if (que.Front() != stk.Top()) {

      isPalindrome = false;

    } else {

      que.remove();

      stk.Pop();

    }

}

    if (isPalindrome) {

      cout << "\nEntered String is a Palindrome !" << endl;

      return true;

    } else {

      cout << "\nEntered String is NOT a Palindrome !" << endl;

      return false;

    }

}

int main() {

// declare variables

string testStr;

cout << "Please Enter the String to be tested for Palindrome: ";

getline(cin, testStr);

if (testStr.empty())

{

    return 0;

}

palindromeTest(testStr);

}

Output #1:

Output #2:


Related Solutions

In Java A palindrome is a word or sequence of characters which reads the same backward...
In Java A palindrome is a word or sequence of characters which reads the same backward and forward, such as madam, dad, racecar, 5885. In java Write a program that asks user to enter a word and prints if the word is a palindrome or not.
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
HTML 7.20 A palindrome is a number or a text phrase that reads the same backward...
HTML 7.20 A palindrome is a number or a text phrase that reads the same backward and forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a script that reads in a five-digit integer and determines whether it’s a palindrome. If the number is not five digits long, display an alert dialog indicating the problem to the user. Allow the user to enter a new value after dismissing the alert dialog....
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction" Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "The number is a palindrome." If it is not then print "The number is NOT a palindrome" Make sure to use an array Allow input...
(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT