Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a line of text, changes each uppercase letter to lowe

ID: 3681474 • Letter: W

Question

Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and into a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). You use templates stack, queue , functions from them, string::length() function, also use getline_portable() function for Eclipse instead of getline(). Function getline_portable() is provided. You give option to user ignore or not ignore spaces ‘ ‘ in the line of text. For example: Check if a line is a palindrome. Ignore spaces? y/n y Input line to check a W rt r wa Inputed line {a W rt r wa} is a palindrome Check if a line is a palindrome. Ignore spaces? y/n n Input line to check a w rtr wa Inputed line {a w rtr wa} is not a palindrome Input line to check see you y o u e e s Inputed line {see you y o u e e s} is not a palindrome Check if a line is a palindrome. Ignore spaces? y/n y Input line to check s ee you uoy e e s Inputed line {see you uoy e e s} is a palindrome

Explanation / Answer

#include<iostream>
#include <fstream>
#include<string>
#include <algorithm>
#include <iomanip>
#include <cctype>

using namespace std;

const int MAX=50;    // initialize max string size of 50 characters
typedef char StackElement; // define StackElement
typedef char QueueElement; // define QueueElement
class Stack
{
public:
            Stack(){top=-1;arr[MAX]=0;}   // default stack constructor
                        void push(StackElement & ch); // push function
                        StackElement topNpop();         // top and pop functions combined
                        bool empty() const;            // empty function
private:
                        StackElement arr[MAX];    // define char array
                        int       top;                  // define int top
           
};
/*******************************************
FUNCTION: push()
DESCRIPTION: Pushes an element onto the stack
PRECONDITION: Waiting for function call
POSTCONTION: New element character on top of stack
*******************************************/
inline void Stack::push(StackElement & ch)
{
            if(top<MAX)
            {
                        top++;          // increment top
                        arr[top]=ch; // push onto stack
            }
            else
            {
                        cout<<"Stack is full. "; // display stack is full
            }


}
/*
FUNCTION: topNpop()
DESCRIPTION: Reads and pops top element off the stack
PRECONDIION: Waiting for function call
POSTCONDITION: One element read and removed fromt he stack
RETURN: Top element from stack
*/

inline StackElement Stack::topNpop()
{
            if(top>-1)
            {
                        return(arr[top]); // returns top element
                        top--;                                     // remove froms stack
            }
            else
            {
                        cout<<"Stack is empty. "; // display stack is empty
                        return(0);

}

}
/*
FUNCTION: empty()
DESCRIPTION: returns result value if stack is empty
PRECONDITION: result=false
POSTCONDITION: result may be true or remain false
RETURN: result if true or false
*/                               
inline bool Stack::empty() const
{
            bool result=false;   // initialize bool as false
            if (top==-1)        
            {
                        result=true;        // if top is -1 return result true
                        return(result);
            }
            else
            {
                        return(result);     // else return false
            }
}
class Queue                           // Queue class
{
public:
                        Queue(){front=0, back=0;arr[MAX]=0;}   // Queue default constructor
                        void addq(QueueElement & ch);          // define addq
                        QueueElement frontNremoveq();         // define frontNremove
private:
                        QueueElement arr[MAX];            // initialize QueueElement array
                        int front, back;                  // initialize int front and back
           
};
/*
FUNCTION: addq()
DESCRIPTION: adds an element onto the queue
PRECONDITION: Waiting for element to add
POSTCONDITION: New element now on the queue
*/
inline void Queue::addq(QueueElement &ch)
{
            if(front!=(back+1)%MAX)
            {
                        arr[back]=ch;     // add element to back of queue
                        back=(back+1)%MAX;
            }
            else
            {
                        cerr<<"Error Queue is full "; // display queue is full
            }
}
/*FUNCTION: frontNremoveq()
DESCRIPTION: reads and removes front element from queue
PRECONDITION: front pointing to front of queue
POSTCONDITION: front element is returned and then incremented
*/
inline QueueElement Queue::frontNremoveq()
{
            if(front!=back)
            {
                        return(arr[front]);    // return front element
                        front++;                                            // remove front element
            }
            else
            {
                        cout<<"Queue is empty. "; // display queue is empty
                        return(0);
            }
}

/*MAIN*/
int main()
{
            Stack S;         // initialize stack
            Queue Q;      // initialize queue
            string s ;
            int i=0;          // initialze int 'i'
            char string[MAX]; // initialize char string
            bool RESULT=false; // initilize bool RESULT to false
           

strcpy(string," ");
//transform(s.begin(),s.end(),s.begin(),toupper);

cout << "Please enter a line of text: " << endl;

cin.getline (string,100);

while(string[i]!=NULL)
            {

            S.push(string[i]); // push chars individually from string to

            Q.addq(string[i]);             // stack and queue

            i++;      // next char

     }

     while(i>0)

     {

            if(S.topNpop()==Q.frontNremoveq()) // compare each element from

            {                                                                         // stack and queue

                    RESULT=true; // if same for all chars return true

            }

            else

            {

                    RESULT=false; // if not same for any char break and             return false

                    break;

            }

            i--;

     }      

if(RESULT==true)

     {

            cout<<string<<" This is a palindrome "; // display if true

     }

     else

     {

            cout<<string<<" This is not a palindrome "; // display if false

     }

    

            cout<<s<<s<<endl;

     return 0;

}

OUTPUT:

1.Hi there

Hi there-this is not a palindrome

2. HOW are You?

how ARE yOU? (UPPERCASE TO LOWERCASE)