Question

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

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 c++ language and use own queue class or class provided. plese do not use queue library. please use functions.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

program in c++

#include <cctype>

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

#define MAX 1000

class Stack

{

int top;

public:

string a[MAX]; //Maximum size of Stack

Stack() { top = -1; }

bool push(string x);

bool isEmpty();

string pop();

};

bool Stack::push(string x)

{

if (top >= (MAX-1))

{

cout << "Stack Overflow";

return false;

}

else

{

a[++top] = x;

cout<<x <<" pushed into stack\n";

return true;

}

}

string Stack::pop()

{

if (top < 0)

{

cout << "Stack Underflow";

return 0;

}

else

{

string x = a[top--];

return x;

}

}

class Queue {

private:

int i;

string arr_queue[MAX];

int rear;

int front;

public:

Queue() {

rear = 0;

front = 0;

}

void insert(string item) {

if (rear == MAX)

cout << "\n Queue Overloaded !";

else {

cout << item<<" Inserted into queue";

arr_queue[rear++] = item;

}

}

void display() {

cout << "\n## Queue Size : " << (rear - front);

for (i = front; i < rear; i++)

cout << "\nPosition : " << i << " , Value : " << arr_queue[i];

}

};

int main()

{

string line;// to hold line

std::ifstream file("input.txt");

std::string str;

class Stack s;

class Queue q;

while (std::getline(file, line))

{

cout<<"Read Line ::"<<line<<"\n";

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

{

line[i] = tolower(line[i]);

}

s.push(line);

q.insert(line);

bool isPalindrome = true;

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

{

isPalindrome *= line[i] == line[line.length() - 1 - i];

}

if(isPalindrome)

{

cout << "\n Line is a palindrome!" << endl << endl;

}

else

{

cout << "\n Line isn't a palindrome!" << endl << endl;

}

}

q.display();

cout<<"\n"<<s.pop() << " Popped from stack\n";


return 0;

}

/*

output

Read Line ::bhushan
bhushan pushed into stack
bhushan Inserted into queue
Line isn't a palindrome!

Read Line ::madam
madam pushed into stack
madam Inserted into queue
Line is a palindrome!

Read Line ::mahajan
mahajan pushed into stack
mahajan Inserted into queue
Line isn't a palindrome!


## Queue Size : 3
Position : 0 , Value : bhushan
Position : 1 , Value : madam
Position : 2 , Value : mahajan
mahajan Popped from stack

input.txt (file)

bhushan
madam
mahajan

*/

Add a comment
Know the answer?
Add Answer to:
Write a program that reads a line of text, changes each uppercase letter to lowercase, and places...
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
  • Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowerca...

    Write a simple program in C++ 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). DO NOT USE THE STACK LIBRARY, QUEUE LIBRARY, OR ANY OTHER LIBRARIES NOT COVERED YET.

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and 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 program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and 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 program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

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