Question

You're to write a C++ program that analyzes the contents of an external file called data.txt....

You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace 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.

In order to derive all these vital statistics, you'll have to write a function to do the dirty 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);

So what's the first thing to do? I recommend drawing a structure chart! This will help clarify how the ReadFile function is to be called, and what its logical inputs and outputs are. A few minutes spent on the chart will help you move closer to the coding phase more easily.

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). As characters are read from the input stream, you'll need to recognize whether they're uppercase, lowercase, digits, punctuation characters, or whitespace characters (e.g., blank, tab, newline, etc.). 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. (don't forget to #include the header file cctype). Only use cctype, fstream, and iostream.

Once you've got an executable, you should try altering the contents of the file data.txt and running your program again so you can see if everything is working correctly. Try different versions that contain random text, only digits, only lowercase characters, only whitespace, or even a completely empty file.

Sample run:

File "data.txt" contains the following:
  Uppercase letters:               158
  Lowercase letters:              1765
  Digits:                           17
  Punctuation characters:          948
  Whitespace characters:          1447
  Total characters read:          4335
0 0
Add a comment Improve this question Transcribed image text
Answer #1

aboutComputer.txt

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.

_________________
#include <fstream>
#include <iostream>
#include <cctype>

using namespace std;

int ReadFile(ifstream& inFile, int& totalUpper, int& totalLower, int& totalDigits, int& totalPunct,
int& totalSpace);
int main()
{

// Declaring variables

int totalChars = 0, totalUpper = 0, totalLower = 0, totalDigits = 0;
int totalPunct = 0, totalSpace = 0;
// defines an input stream for the data file
ifstream inFile;


// Opening the input file
inFile.open("aboutComputer.txt");


// checking whether the file name is valid or not
if (inFile.fail())
{
cout << "** File Not Found **";
return 1;
}
else
{
// calling the function
totalChars = ReadFile(inFile, totalUpper, totalLower, totalDigits, totalPunct, totalSpace);

// Closing the intput file
inFile.close();

// Displaying the output
cout << "File \"aboutComputer.txt\" contains the following:" << endl;
cout << "Uppercase letters:" << totalUpper << endl;
cout << "Lowercase letters:" << totalLower << endl;
cout << "Digits:" << totalDigits << endl;
cout << "Punctuation characters:" << totalPunct << endl;
cout << "Whitespace characters:" << totalSpace << endl;
cout << "Total characters read:" << totalChars << endl;
}


return 0;
}

// Function wehich will read the data from the file and check each character type
int ReadFile(ifstream& inFile, int& totalUpper, int& totalLower, int& totalDigits, int& totalPunct,
int& totalSpace)
{
char ch;
int totalChars = 0;
while (!inFile.eof())
{


inFile.get(ch);
totalChars++;
if (isupper(ch))
{
totalUpper++;
}
if (islower(ch))
{
totalLower++;
}
if (isdigit(ch))
{
totalDigits++;
}
if (isspace(ch))
{
totalSpace++;
}
if (ispunct(ch))
{
totalPunct++;
}
}
return totalChars;
}

__________________

Output:


_____________Could you rate me well.Plz .Thank You

Add a comment
Know the answer?
Add Answer to:
You're to write a C++ program that analyzes the contents of an external file called data.txt....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to read the contents of data.txt file, and determine the smallest value (min),...

    Write a program to read the contents of data.txt file, and determine the smallest value (min), largest value (max), and the average value (ave). The minimum, maximum, and average values must be calculated in the function calc(). Remember to declare the stdlib.h library, so that you can use the atof function, which will convert the number in the text file into float number. Also, remember to check if there is an error opening the file or not. If there is...

  • I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration...

    I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration Data Types Reformat is the shell of a program designed to read characters and process them in the following way Lowercase character Uppercase character Digit Blank Newline Any other character Converts to uppercase and writes the character Writes the character Writes the digit Writes a blank Writes newline Does nothing / Program Reformat reads characters from file DataIn and // writes them to DataOut...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find...

    python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: ## 1. Number of uppercase characters in the file ## 2. Number of lowercase characters in the file ## 3. Number of digits in the file. ## 4. Number of whitespace characters in the file ## ## print("Number of...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • i need the answer for this python question using dictionary method please!!! points) Attached to the...

    i need the answer for this python question using dictionary method please!!! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: 1. Number of uppercase characters in the file 2. Number of lowercase characters in the file 3. Number of digits in the file. 4. Number of whitespace characters in...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • Create a Python script file called hw.py. Then import the module random: from random import *...

    Create a Python script file called hw.py. Then import the module random: from random import * Thanks in advance! Ex. 1. Write a function called cleanLowerWord that receives a string as a paramcter and retums a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this,...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT