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

HW10: File Analysis In this assignment you\'re to write a program that analyzes

ID: 3728884 • Letter: H

Question

HW10: File Analysis

In this assignment you're to write a program that analyzes the contents of an external file called dracula.txt. (It is an eBook titled Dracula by Bram Stoker.) The program you write will open dracula.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation, whitespace, lines, periods and question mark characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to the caller. An example of the run is listed towards the end.

I have already created for you the directory

In order to derive all these statistics, you'll have to write a function to do the work. The name of the function is ReadFile and here's what a sample function call looks like:

totalChars = ReadFile(inFile, totalUpper, totalLower, totalDigits,
totalPunct, totalSpace, totalLines, totalPeriods, totalQuestion);

Trying creating a structure chart like we did in class to understand how the program will work.

The ReadFile function is going to examine every single character in the file, including whitespace characters (which means you'll have to use character I/O, which means you'll want to use the .get(), .put() and .eof() functions, which means you might want to look at p.340 and p.355 in your textbook). As characters are read from the input stream, you'll need to recognize whether they're uppercase, lowercase, digits, punctuation characters, whitespace characters (e.g., blank, tab, newline, etc.), newlines, periods, or question marks. You could certainly write the code to do this yourself (it's easy to do), but the good news is that you don't have to! Recall that the standard libraries already have some character testing functions for you to use, like isdigit, isupper, islower, isspace, etc. Look on p.1042 in your textbook, these functions are extremely easy to use (don't forget to #include the header file cctype). It would be worth your while to write some quick code to experiment with these functions so you know what's available and how to use it.  Now the key thing is how will you figure out if the character is a newline, period, or a question mark?  I suggest you look closely at the character testing functions in the standard library and see how you can use them to your advantage. 

To help you get started, I went ahead and created a HW10 subdirectory for you and put starter kit in there called FileAnalysis.tar.gz. This is a compressed archive file, much like a zip file, it contains files that need to be extracted. To extract the file(s), use this command:

   tar xvzf FileAnalysis.tar.gz 

This will extract the file(s) contained within the archive into the present working directory. You should then find a text file called dracula to use in your analysis.

NOTE: You only need to extract the files once! If you extract the text file, write something in it, and then extract again at a later point in time, the original text file in the archive will be extracted and overwrite any changes that you've already written!

You can leave your code in a file called file_analysis.cpp in the HW10 subdirectory, and we'll review the program together at our next class session.

 

==============================
Sample Run of FileAnalysis.exe
==============================
 
File "dracula.txt" contains the following:
  Uppercase letters:                   20592
  Lowercase letters:                   638144
  Digits:                                       729
  Punctuation characters:         33901
  Whitespace characters:         189703
  Lines:                                       15973
  Total Periods:                          8505
  Total Question Marks:            492
  Total characters read:             883160

Explanation / Answer

CODE:

#include <iostream>

#include <string.h>

#include <cctype>

#include <fstream>

using namespace std;

int ReadFile(ifstream* infile, int* totalUpper, int* totalLower, int* totalDigits, int* totalPunct, int* totalSpaces, int* totalLines, int* totalPeriods, int* totalQuestion);

int main()

{

ifstream infile("C:\Users\silpa.rangadhamappa\Documents\Chegg\test.txt");

int totalUpper=0, totalLower=0, totalDigits=0, totalPunct=0, totalSpaces=0, totalLines=0, totalPeriods=0, totalQuestion=0;

int totalcharacters = ReadFile(&infile, &totalUpper, &totalLower, &totalDigits, &totalPunct, &totalSpaces, &totalLines, &totalPeriods, &totalQuestion);

cout<<"Uppercase Letters: "<<totalUpper<<endl;

cout<<"Lowercase Letters: "<<totalLower<<endl;

cout<<"Digits: "<<totalDigits<<endl;

cout<<"Punctuatin characters: "<<totalPunct<<endl;

cout<<"Whitespace characters: "<<totalSpaces<<endl;

cout<<"Lines: "<<totalLines<<endl;

cout<<"Total Periods: "<<totalPeriods<<endl;

cout<<"Total Question mark: "<<totalQuestion<<endl;

cout<<"Total characters read: "<<totalcharacters<<endl;

return 0;

}

int ReadFile(ifstream* infile, int* totalUpper, int* totalLower, int* totalDigits, int* totalPunct, int* totalSpaces, int* totalLines, int* totalPeriods, int* totalQuestion)

{

int totalchar=0;

char ch;

if(infile)

{

while(!infile->eof())

{

infile->get(ch);

if(isupper(ch))

*totalUpper += 1;

if(islower(ch))

*totalLower += 1;

if(isdigit(ch))

*totalDigits += 1;

if(ch == '!')

*totalPunct += 1;

if(isspace(ch))

*totalSpaces += 1;

if(ch == ' ')

*totalLines += 1;

if(ch == '?')

*totalQuestion += 1;

if(ch == '.')

*totalPeriods += 1;

totalchar++;

}

}

return totalchar;

}