Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Platform: C++ ADD A FUNCTION. for line and letter count that reads the text from

ID: 3774985 • Letter: P

Question

Platform: C++

ADD A FUNCTION. for line and letter count that reads the text from an input file, 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.

You must use (and NOT change) the variables and functions given to you in the code. Add your own function/viariables as needed.

---------C++ CODE FOLLOWS-----


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <string>
#include <cstdio>

using namespace std;

typedef struct data{char ch; int Count;}data; //declaration

void copyText(char ch, struct data *List, int &char_count);
void writeTotal(ofstream& outFile, data *List,int char_count);
void Sort_array(struct data *List);

int main()
{
    ///Variable declaration
    ifstream inFile; //input file stream variable
    ofstream outFile; //output file stream variable
    string inputFile; //variable to store the name of the input file
    string outputFile; //variable to store the name of the output file
    //Character Occurence
    int char_count=0; //stores character count of the file
    struct data *List = new data[26]; //store the letter count A-Z...0-26
    char ch; //store the character of file

    ///initialize        array of struct
    for(int i=0;i < 26; i++){
        List[i].ch = 'A' + i;
        List[i].Count = 0;
    }

    //Open the I/O files
    cout << "Enter the input file name: ";
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile.c_str());
    if (!inFile.is_open()){ //If file is failed to open
        cout << "Cannot open the input file." << endl;
        return 0;
    }
    cout << "Enter the output file name: ";
    cin >> outputFile;
    cout << endl;
    outFile.open(outputFile.c_str());

    //Reading file character by character
    while(inFile.get(ch)){
        copyText(ch,List,char_count); //calling function copyText()
    }

    //sort array of char and their count
    Sort_array(List);
    //Write data to output file
    writeTotal(outFile,List,char_count);

    cout<<"Done with writing data into output file"<     //closing both input and output file
    inFile.close();
    outFile.close();

    return 0;
}


//incrementing the count of alphabet
void copyText(char ch, struct data *List, int &char_count){ //outputs Original message contents
    //If alphabet belongs to a..z or A..Z
    if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')){
        List[toupper(ch) - 'A'].Count += 1 ;
        char_count++; //counting character
    }
}

//Writing required data into output file
void writeTotal(ofstream& outFile, data *List,int char_count){ //outputs to output file line & letter count

    int i;

    outFile << "Number of chars = "<     outFile << "Five Most Used Characters: "<

    for(int i=0; i < 5; i++){
        outFile <<"'"<

    outFile << "Characters Not Used: ";
    for(int i=0; i < 26; i++){
        if(List[i].Count == 0){
            outFile << List[i].ch <<", ";
        }
    }
    outFile << endl;
    outFile << List[i].ch<

}
///counting and sorting all the characters   **characterCount**
//bubble sort of array using count of alphabets as parameter
void Sort_array(struct data *List){
    struct data temp;

    for(int i = 1; i < 26; i++){
        for(int j = 0; j < (26 - i); j++){
            if(List[j].Count < List[j+1].Count){
                temp.ch = List[j].ch;
                temp.Count = List[j].Count;
                List[j].ch = List[j+1].ch;
                List[j].Count = List[j+1].Count;
                List[j + 1].ch = temp.ch;
                List[j + 1].Count = temp.Count;
            }
        }
    }
}

----------------END OF C++ ---------------

Example of the input.txt is bellow this line . You can save this file as a .txt and run my code. NOTE: The file must have a line return after the last character, in order for our example code to run properly. If you don’t have that blank line at the end of the input file, the program will not perform as intended and behave unpredictably.

---------------Input.txt begins--------------------------------

9 8 7 6 5 You must have a line return after the
last character in your message, in order
for our example code to run properly. Note
that there are 8 lines of text in this file
including an additional mandatory "blank line"...otherwise
your system may hang up and create huge output files
the intentional line return and final blank line follows.

-----------------END OF INPUT.TXT -------------------------

------------OUTPUT EXAMPLE FOLLOWS---------

_____________________
Character Occurence
- - - - - - - - - - - -
A   count = 24   B count =   2     C count =   5
D   count =   8   E count = 33    F count =   7
G   count =   4 H count = 10   I count = 19
J count =   0    K count =   2      L count = 18
M   count =   6   N count = 26     O count = 18
P count =   5     Q count =   0      R count = 21
S count = 11    T count = 25     U count = 13
V count =   1    W count =   2      X count =   2
Y count =   7     Z count =   0

Number of lines = 7

________________________________________
Original Message Contents
- - - - - - - - - - - -
9 8 7 6 5 You must have a line return after the
last character in your message, in order
for our example code to run properly. Note
that there are 8 lines of text in this file
including an additional mandatory "blank line"...otherwise
your system may hang up and create huge output files
the intentional line return and final blank line follows.

------------END OF OUTPUT EXAMPLE ---------

Explanation / Answer

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file line & letter count
{
   outtext<<"___________________________________________________________"<<endl;
   outtext<<"Character Occurence                       "<<endl;
   outtext<<"___________________________________________________________"<<endl;  
   for( int i = 0 ; i < 26 ; i = i + 3)
   {
       if( i == 24 )
           outtext<<(char)(list[i]+'A')<<" count = "<< list[i]<<" "<<(char)(list[i+1] + 'A' )<< " count = "<<list[i+1]<<endl;
       else
       outtext<<(char)(list[i]+'A')<<" count = "<< list[i]<<" "<<(char)(list[i+1] + 'A') << " count = "<<list[i+1]<<" "<<(char)(list[i+2] + 'A') << " count = "<<list[i+2]<<endl;
   }
   outtext<<"Number of lines = "<<lc<<endl;
}