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 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
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....
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...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT