Answered step by step
Verified Expert Solution
Question
1 Approved Answer
//************************************************************* // Author: D.S. Malik // // Program: Line and Letter Count // This programs reads a text, outputs the text as is, and also
//************************************************************* // Author: D.S. Malik // // Program: Line and Letter Count // This programs reads a text, outputs the text as is, and also // prints the number of lines and the number of times each // letter appears in the text. An uppercase letter and a // lowercase letter are treated as being the same; that is, // they are tallied together. //************************************************************* #include#include #include using namespace std; void initialize(int& lc, int list[]); void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]); void characterCount(char ch, int list[]); void writeTotal(ofstream& outtext, int lc, int list[]); int main() { //Step 1; Declare variables int lineCount; int letterCount[26]; char ch; ifstream infile; ofstream outfile; infile.open("textin.txt"); //Step 2 if (!infile) //Step 3 { cout << "Cannot open the input file." << endl; return 1; } outfile.open("textout.out"); //Step 4 initialize(lineCount, letterCount); //Step 5 infile.get(ch); //Step 6 while (infile) //Step 7 { copyText(infile, outfile, ch, letterCount); //Step 7.1 lineCount++; //Step 7.2 infile.get(ch); //Step 7.3 } writeTotal(outfile, lineCount, letterCount); //Step 8 infile.close(); //Step 9 outfile.close(); //Step 9 return 0; } void initialize(int& lc, int list[]) { int j; lc = 0; for (j = 0; j < 26; j++) list[j] = 0; } //end initialize void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) { while (ch != ' ') //process the entire line { outtext << ch; //output the character characterCount(ch, list); //call the function //character count intext.get(ch); //read the next character } outtext << ch; //output the newline character } //end copyText void characterCount(char ch, int list[]) { int index; ch = toupper(ch); //Step a index = static_cast (ch) - static_cast ('A'); //Step b if (0 <= index && index < 26) //Step c list[index]++; } //end characterCount void writeTotal(ofstream& outtext, int lc, int list[]) { int index; outtext << endl; outtext << "The number of lines = " << lc << endl; for (index = 0; index < 26; index++) outtext << static_cast (index + static_cast ('A')) << " count = " << list[index] << endl; } //end writeTotal
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started