Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 1: Write a program to count letters in a file Take a look at a file called testfile.txt. Your first job is to write

Step 1: Write a program to count letters in a file

Take a look at a file called testfile.txt. Your first job is to write a program to read that file and count the number of times each letter occurs in the file, disregarding case (i.e. 'A' and 'a' count the same) and ignoring punctuation or other non-letter characters. That means there are 26 characters you need to count.

Your program should accept the name of the file to process as its only command line argument. That is, if your executable program is called count_letters, you would run it like this:

./count_letters testfile.txt

Your program should work by opening the file to read as an ifstream. Once you have the file opened as an ifstream, you should pass the ifstream to a function like this:

void count_letters(ifstream&, int*);

This function should do the work of actually reading through the ifstream that represents your opened file and counting the letters there, storing the counts in a pre-allocated array of integers thats passed in.

Step 2: Save your letter counts to a new file

Now, add new functionality to your program to take the letter counts you computed earlier and write them into a new file as tab-separated values. Your program should take the name of the file to which to write the letter counts as its second command line argument, e.g.:

./count_letters testfile.txt counts.tsv

To add this functionality to your program, you should open the file to which to write as an ofstream. Once you have the ofstream, you should pass it along with the letter counts you computed to a new function that actually writes the counts to the file: void write_counts(ofstream&, int*);

This function should do the work of actually writing the letter counts to the file represented by the ofstream. In particular, the counts should be written in a format like this, with a letter and its count on a single line, separated by a tab (these are not the actual counts, theyre just an example):

a 13

b 3

c 20

d 16

e 31

...

image text in transcribed

testfile.txt FINISHED FILES ARE THE RESULT OF YEARS OF SCIENTIFIC STUDY COMBINED WITH THE EXPERIENCE OF YEARS The necessity of training farm hands for first class farms in the fatherly handling of farm live stock is foremost in the eyes of farm owners. Since the forefathers of the farm owners trained the farm hands for first class farms in the fatherly handling of farm livestock, the farm owners feel they should carry on with the family tradition of training farm hands of first class farmers in the fatherly handling of farm live stock because they believe it is the basis of good fundamental farm management

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Different formulas for mathematical core areas.

Answered: 1 week ago