Write a program that opens a text file (name the textfile "problem4.txt") and reads its contents into a queue of characters. The user must then enter a character they are looking for. The program should then dequeue each character and count the number of characters that are equal to what the user is looking for. Output the count of the character or lack thereof in a second file (name the textfile "resultsp4.txt").

Answers

Answer 1

Answer:

See explaination

Explanation:

/* reading a text file Character by Character

* Enter the character to Queue

* Search Specific character and Printingits occurrence

* otherwise Lack thereof

*/

// Include header file for File Reading

#include <iostream>

#include <fstream>

// Maximum no of character in a file

# define N 50

using namespace std;

// Queue Data Structure

typedef struct

{

char arr[N];

int front;

int rear;

int size;

}Queue;

// Function Prototypes

void initialize(Queue*);

void Enqueue(Queue*,char);

char Dequeue(Queue*);

int isEmpty(Queue*);

int isFull(Queue*);

int Qsize(Queue*);

int main () {

// Variable Declaration

char ch;

char searchchar;

// Allocating memeory for the Queue.

Queue *Q=(Queue *)malloc(sizeof(Queue));

// Reading from the file

fstream fin("problem4.txt", fstream::in);

//initialize Queue with front rear and size

initialize(Q);

//Looping through the file and Enqueue it in Queue

while (fin >> noskipws >> ch) {

Enqueue(Q,ch);

}

// close the opened file.

fin.close();

int i=0;

int n=Qsize(Q);

int count=0;

//Asking user for Input

cout << "Please character to found ";

cin >> searchchar;

while (i<n) {

if(Dequeue(Q)==searchchar)

count++; // if found increase the count

i++;

}

// Total Count od character searched

cout << "Total Count of Character " << count << endl;

// Output Total Count of Character to a file named resultsp4.txt

ofstream outfile;

outfile.open("resultsp4.txt");

cout << "Writing to the file" << endl;

if (count>0)

outfile << "The total occurence of the " << searchchar <<" is "<< count <<endl;

else

outfile << "lack thereof " << searchchar <<endl;

// close the opened file.

outfile.close();

return 0;

}

void initialize(Queue *Q)

{

Q->front=-1;

Q->rear=-1;

Q->size=0;

}

void Enqueue(Queue * Q,char ch)

{

(Q->rear)+=1;

(Q->arr[Q->rear])=ch;

(Q->size)++;

}

char Dequeue(Queue * Q)

{

char ch=Q->arr[Q->front];

(Q->front)++;

(Q->size)--;

return ch;

}

int isEmpty(Queue * Q)

{

if ((Q->size)==0)

return 1;

else

return 0;

}

int isFull(Queue * Q)

{

if ((Q->size)==N)

return 1;

else

return 0;

}

int Qsize(Queue * Q)

{

return Q->size;

}


Related Questions

Other Questions
Use the graph to identify the independent and thedependent variable. Check all that apply.OThe two variables are Eli and 5.The two variables are meters and seconds.0 Meters are graphed on the x-axis.Meters are graphed on the y-axis.0 Seconds is the independent variable.0 Seconds is the dependent variable.I need help does the infinite geometric series diverge or converge? Explain.1/5 + 1/10 + 1/20 + 1/40 A) it diverges it has a sumB) it diverges doesnt have a sum C) it converges it has a sumD) it converges doesnt have a sum Which statement correctly describes the diagram? On a coordinate plane, triangle A is reflected across the x-axis to form triangle B. Triangle A is rotated to form triangle C. Triangle B is a reflection of triangle A across the x-axis. Triangle C is not a reflection of triangle A.. Triangle B is a reflection of triangle A across the y-axis. Triangle C is a reflection of triangle A across the line y = x + 3. Triangle B is a reflection of triangle A across the y-axis. Triangle C is not a reflection of triangle A. Triangle B is a reflection of triangle A across the x-axis. Triangle C is a reflection of triangle A across the line y = x + 3. 70 points, answer ASAP The most important city to the Mayan people, which contains the ruins of a number of Mayan temples, wasChichn Itz.Mexico City. How were all living things classified Which sentence uses details about sound to describe the setting?O A. "It's on private property, surrounded by fields that were growingdark under the sunset and rising moon."B. "I stopped every minute or so to catch my breath and pull tinycactus spikes from my toes."O c. "Finally I stood still, listening to the barely audible whoosh-whoosh-whoosh from the propellers ..."O D. "There's a small diner up on a hill, and I went inside ..." A recipe calls for 3 teaspoons of orange juice. A can of orange juice is 20 fluid ounces. How many teaspoons of juice are in the can? My child needs help and I don't get it. Help! What are the coordinates of the first alien who needs to be rescued? What is the slope of the line that provides the correct path to rescue the alien? How did the US justify war in Vietnam? The first women's rights convention in the United States was theA National American Woman Suffrage ConventionB. National Woman's Party ConventionC. Seneca Falls ConventionD. Declaration of Rights ConventionPlease select the best answer from the choices providedOAOB What is the measure of RMN?117[Not drawn to scale]27 degrees 63 degrees 117 degrees 163 degrees A triangular pyramid has a base area of 24 square meters and a height of 4 meters. What is the volume of the pyramid? The wife of a silk merchant, the famous Mona Lisa is thought to be a portrait of Lisa Gherardini del Giocondo.Which of the following is the most clear and concise way to write this sentence so the focus is on the Mona Lisa? If itis correct, mark "correct as is."A. The wife of a silk merchant, Lisa Gherardini del Giocondo is thought to be the subject of the famousMona Lisa.B. The famous Mona Lisa is thought to depict Lisa Gherardini del Giocondo, the wife of a silkmerchantLisa Gherardini del Giocondo is thought to be depicted in the famous Mona Lisa, the wife of a silkmerchantC.correct as is Which image shows a translation of the green quadrilateral JKLM three units to the right and one unit down? Harry bought a used bike for 1,050. Before purchasing the bike, Harry had a total of 1250 I. His savings account what percentage of his account did Harry spend on a diet bike? It is the largest object.(1 Point)asteroidcometmeteor what is the length of the diameter of the circle The Gastrointestinal system is also known as the what?nervous centerDigestive systemCirculatory systemEndocrine SystemNeed ASAP Multiply (2x^2-3x+1)*(x^2-4x-3) Structure Click the link and then read about the parts of a bacterial cell. Pay attention to the analogies that are presented. Bacteria Cell Answer these questions based on what you learned from the passage. You can assume any body structure comes from a human. The flagella are like . The pilli are like . The capsule is like . The cell wall is like the . The ribosomes are like . The nucleoid is like the . The cell membrane is like . The cytoplasm is like happens. Steam Workshop Downloader