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

C++ user defined functions and arrays. I need to ADD a function that returns the

ID: 3774875 • Letter: C

Question

C++ user defined functions and arrays.

I need to ADD a function that returns the character statistics of an input file and I don't know how to go about doing it. I have "hit the wall" with this part. I tried using isalpha, isdigit, isalnum,….. functions and I couldn’t make it work.

I hope I have provided all the information needed for assistance. I am including: my code and an input file example. THANK YOU!

What I need: A function that gives me the following statistics about an alpha-numeric input file (file example is at the bottom of this question). I also need the percentage.

-        Total of characters processed (alpha + numeric + special character)

-        Total of digits processed

-        Total of Upper case letters processed (A-Z)

-        Total of hexadecimal digits processed

-        Total of Punctuation characters processed (, . ; ’ : ; )

-        Total of Control character processed

-        Total of lower case letters processed (a-z)

-        Total of all alphanumeric characters processed (A-Z, a-z, and 0-9)

-        Total of all printable characters

-        Total of blank spaces

OUTPUT TO THE SCREEN, EXAMPLE:

Character Stats: 346 total characters processed.

TYPE         COUNT     PERCENT

-Digits             6                0.017%

-UpperCase     2               0.006%

-HexDigit      85             0.246%

-Punctuation   8                0.023%

-Control          7                0.020%

-LowerCase   267            0.772%

-AlpahNumer   275           0.795%

-Printable        339          0.980%

-Space             63             0.182%

________________________________________

---------------MY C++ CODE FOLLOWS (code runs without bugs. It will ask you for your input file and the name of your output file)-----------

#include

#include

#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()

{
///Variable declaration

    ifstream inFile;    //input file stream variable
    ofstream outFile;   //output file stream variable
    char inputFile[51]; //variable to store the name of the input file
    char outputFile[51];    //variable to store the name of the output file

    //Character Occurence
    int lineCount; //stores the line count
    int letterCount[26]; //store the letter count A-Z...0-26
    char ch; //store the characters

///Open the I/O files

    cout << "Enter the input file name: ";
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile);
    if (!inFile)
    {
        cout << "Cannot open the input file." << endl;
        return 1;
    }

    cout << "Enter the output file name: ";
    cin >> outputFile;
    cout << endl;
    outFile.open(outputFile);

initialize(lineCount, letterCount);             //initialize lineCount & letterCount


    inFile.get(ch);                                 //read the first character
    while (inFile)
    {
        copyText(inFile, outFile, ch, letterCount); //process the lines
        lineCount++;                                //increment the line count
        inFile.get(ch);                             //read the next character
    }

    writeTotal(outFile, lineCount, letterCount);    //output # of lines and Char occurrence


    inFile.close();
    outFile.close();

    return 0;
}

void initialize(int& lc, int list[]) //initialize lineCount& letterCount to 0
{
    int j;
    lc = 0;

    for (j = 0; j < 26; j++)
        list[j] = 0;
}

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) //outputs Original message contents
{

    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
}

void characterCount(char ch, int list[]) //counting character occurrence
{
    int index;

    ch = toupper(ch);                       //convert letter to uppercase

    index = static_cast(ch)
            - static_cast('A');        //Find the index array for this letter

    if (0 <= index && index < 26)           //IF the index is valid, add it to the appropriate count
        list[index]++;
}

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file line & letter count
{
    int index;

    outtext << endl << endl;
    outtext << "Number of lines = " << lc << endl;

    outtext << endl << endl;
    outtext << "____________________________________________"<< endl;
    outtext << "Character Occurrence"<< endl;
    outtext << "-------------------------"<< endl;
    for (index = 0; index < 26; index++)
        outtext << setw(10)
        << static_cast(index + static_cast('A'))
        << " count = "<< list[index] << endl;
        outtext << "____________________________________________"<< endl;
}

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

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 -------------------------

Explanation / Answer

#include std_  characters;

#include std_  digits ;

#include std_  hexadecimal digits;

#include std_ Punctuation characters;

#include std_ out lines;

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()

{
///Variable declaration

    ifstream inFile;    //input file stream variable
    ofstream outFile;   //output file stream variable
    char inputFile[51]; //variable to store the name of the input file
    char outputFile[51];    //variable to store the name of the output file

    //Character Occurence
    int lineCount; //stores the line count
    int letterCount[26]; //store the letter count A-Z...0-26
    char ch; //store the characters

///Open the I/O files

    cout << "Enter the input file name: ";
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile);
    if (!inFile)
    {
        cout << "Cannot open the input file." << endl;
        return 1;
    }

    cout << "Enter the output file name: ";
    cin >> outputFile;
    cout << endl;
    outFile.open(outputFile);

initialize(lineCount, letterCount);             //initialize lineCount & letterCount


    inFile.get(ch);                                 //read the first character
    while (inFile)
    {
        copyText(inFile, outFile, ch, letterCount); //process the lines
        lineCount++;                                //increment the line count
        inFile.get(ch);                             //read the next character
    }

    writeTotal(outFile, lineCount, letterCount);    //output # of lines and Char occurrence


    inFile.close();
    outFile.close();

    return 0;
}

void initialize(int& lc, int list[]) //initialize lineCount& letterCount to 0
{
    int j;
    lc = 0;

    for (j = 0; j < 26; j++)
        list[j] = 0;
}

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) //outputs Original message contents
{

    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
}

void characterCount(char ch, int list[]) //counting character occurrence
{
    int index;

    ch = toupper(ch);                       //convert letter to uppercase

    index = static_cast(ch)
            - static_cast('A');        //Find the index array for this letter

    if (0 <= index && index < 26)           //IF the index is valid, add it to the appropriate count
        list[index]++;
}

void writeTotal(ofstream& outtext, int lc, int list[]) //outputs to output file line & letter count
{
    int index;

    outtext << endl << endl;
    outtext << "Number of lines = " << lc << endl;

    outtext << endl << endl;
    outtext << "------"<< endl;
    outtext << "Character Occurrence"<< endl;
    outtext << "----"<< endl;
    for (index = 0; index < 26; index++)
        outtext << setw(10)
        << static_cast(index + static_cast('A'))
        << " count = "<< list[index] << endl;
        outtext << "-----"<< endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote