Question

In: Computer Science

C++ Write a program that reads a line of text, changes each uppercase letter to lowercase,...

C++

Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto 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). Please use a Queue Class and Stack class.

Solutions

Expert Solution

------------------MyQueue.h----------------

#include<iostream>

using namespace std;

class MyQueue

{

    char *queue;

    int front;

    int rear;

    int max;

   

    public:

       

        MyQueue(int max)

        {

            this->max = max;

           

            // create an int array os size max

            this->queue = new char[ max ];

           

            this->front = -1;

            this->rear = -1;

        }

       

        // destructor

        ~MyQueue()

        {

            delete this->queue;

        }

       

        void enqueue(char i)

        {

            // if the queue is full

            if( rear == max - 1 )

                return;

            else

            {

                // add element to queue

                this->queue[++this->rear] = i;

            }

        }

       

        void dequeue()

        {

            // if queue is empty

            if( this->front != this->rear )

                this->front++;

        }

       

        char top()

        {

            // if queue is empty

            if( this->front == this->rear )

                return -1;

            else

                return this->queue[ this->front + 1 ];

        }

       

        // check if queue is empty

        bool isEmpty()

        {

            return this->front == this->rear;

        }

};

---------------------Stack.h---------------------

#ifndef MY_STACK

#define MY_STACK

#include<iostream>

#include<cstdlib>

#include<cstring>

using namespace std;

class Stack

{

    // declare stack array arr

    char *arr;

   

    // to store the index of the top most element

    int top;

   

    // to store the current capacity of arr

    int size;

   

    public:

       

        // constructor

        Stack();

       

        // constructor

        Stack(int);

       

        // destructor

        ~Stack();

       

        // function to check if the stack is empty

        bool isEmpty();

       

        // function to check if the stack is full

        bool isFull();

       

        // push element intot the stack

        void push(char);

        

        // return the top most element in the stack

        char peek();

       

        // pop element from stack

        void pop();

       

        // resize the array to double the original capacity

        void resize();

       

        // display contents of stack

        void view();

};

#endif

-------------------Stack.cpp--------------------

#include "Stack.h"

// constructor

Stack::Stack()

{

    // declare an array of size 4

    arr = new char[4];

       

    // set initial capacity to 4

    size = 4;

       

    top = -1;

}

// constructor

Stack::Stack(int new_size)

{

    // declare an array of size new_size

    arr = new char[new_size];

       

    // set initial capacity to new_size

    this->size = new_size;

       

    top = -1;

}

// destructor

Stack::~Stack()

{

    delete arr;

    size = 0;

    top = -1;

}

// function to check if the stack is empty

bool Stack::isEmpty()

{

    if(top == -1)

        return true;

    return false;

}

// function to check if the stack is full

bool Stack::isFull()

{

    if(top == size - 1)

        return true;

    return false;

}

void Stack::push(char n)

{

    // if stack is full

    if(isFull())

        // resize the array to double the original capacity

        resize();

       

    arr[++top] = n;

}

// return the top most element in the stack

char Stack::peek()

{

    if(isEmpty())

        return -1;

       

    return arr[top];

}

// pop element from stack

void Stack::pop()

{

    // if stack is empty

    if(isEmpty())

        cout<<"The stack is alreasy empty";

   

    // remove the top most element of the stack

    top--;

}

// resize the array to double the original capacity

void Stack::resize()

{

    // create a new int array double the size of arr

    char *temp = new char[2 * size];

    int i;

   

    for( i = 0 ; i < size ; i++ )

        // cop contents of arr to temp

        temp[i] = arr[i];

   

    // set capacity to double the original capacity

    size *= 2;

   

    // set temp as the new stack array

    arr = temp;

}

// display contents of stack

void Stack::view()

{

    int i;

   

    for( i = 0 ; i <= top ; i++ )

        cout<<arr[i]<<" ";

       

    cout<<endl;

}


-------------------main.cpp----------------

#include "Stack.cpp"

#include "MyQueue.h"

#include<bits/stdc++.h>

bool isPalindrome()

{

    // remains true is string is palindrome

    bool flag = true;

       

    // create queue of size 10000

    MyQueue q(10000);

   

    Stack st;

    string temp;

   

    cout<<"Enter string ; ";

   

    // read complete line

    getline( cin , temp );

   

    // convert string to lower case

    transform(temp.begin(), temp.end(), temp.begin(), ::tolower);

   

    int i, len = 0;

   

    for( i = 0 ; i < temp.length() ; i++ )

    {

        // if the current character is not space

        if( temp[i] != ' ' )

        {

            // convert the current character to lower case and addd to queue

            q.enqueue((char)tolower(temp[i]));

           

            // convert the current character to lower case and addd to stack

            st.push((char)tolower(temp[i]));

        }

    }

    // loop untill the queue is empty

    while( !q.isEmpty() )

    {

        char ch1 = q.top();

        char ch2 = st.peek();

       

        q.dequeue();

        st.pop();

       

        if( ch1 != ch2 )

       {

            flag = false;

           

            break;

        }

    }

       

    return flag;

}

int main()

{

    if( isPalindrome() )

        cout<<"It is palindrome.";

    else

        cout<<"it is not palindrome.";

       

    return 0;

}


Sample Output


Related Solutions

Write a program that reads a line of text input by the user and places each...
Write a program that reads a line of text input by the user and places each word in a TreeSet. Print the elements of the TreeSet to the screen. This will cause the elements to be printed in ascending order. Using Eclipse for this. TreeSetUse.java.
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains...
Write a program in C++ (parking.cc) that reads a group of input lines. Each line contains an A for arrival or a D for departure, which is terminated by a :, and a license plate number, which is terminated by a :. The program should print a message each time a car arrives or departs. When a car arrives, the message should specify when the garage is full. If there is no room for a car, the car simply leaves....
(Please write in C++) Write a program that reads in a line consisting of a student’s...
(Please write in C++) Write a program that reads in a line consisting of a student’s name, Social Security number, user ID, and password. The program outputs the string in which all the digits of the Social Security number and all the characters in the password are replaced by x. (The Social Security number is in the form 000-00-0000, and the user ID and the password do not contain any spaces.) Your program should not use the operator [ ]...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT