1. Engineering
2. Computer Science
3. write a program to count letters in a file cplz...

# Question: write a program to count letters in a file cplz...

###### Question details

Write a program to count letters in a file (C++plz!!!)

Your git repo for this lab comes with a file called testfile.txt. Take a look at this file. Your first job is to write a program to read testfile.txt 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 std::std::ifstream. Once you have the file opened as an std::ifstream, you should pass the std::ifstream to a function like this:

void count_letters(std::ifstream&, int*);

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

Think carefully about how you want to design this function. There’s a way to implement it in such a way that you only have to make one pass reading through the file and don’t have to allocate any additional memory (beyond the std::ifstreamand the array of ints). See if you can figure out how to do that. After you’ve worked on this problem for a little while, your TAs will stop you so they can compare some of your approaches to go over their pros and cons.

Here’s some more documentation that will help you get going with std::ifstream:

• std::ifstream constructor
• C++ Basic file I/O

Also remember that, once you have a file open, std::ifstream works pretty much exactly like std::cin, which you should be familiar with. The main difference is that instead of reading input from the user in the terminal, std::ifstreamreads input from a file.

Write a program to count letters in a file

Your git repo for this lab comes with a file called testfile.txt. Take a look at this file. Your first job is to write a program to read testfile.txt 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 std::std::ifstream. Once you have the file opened as an std::ifstream, you should pass the std::ifstream to a function like this:

void count_letters(std::ifstream&, int*);

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

Think carefully about how you want to design this function. There’s a way to implement it in such a way that you only have to make one pass reading through the file and don’t have to allocate any additional memory (beyond the std::ifstreamand the array of ints). See if you can figure out how to do that. After you’ve worked on this problem for a little while, your TAs will stop you so they can compare some of your approaches to go over their pros and cons.

Here’s some more documentation that will help you get going with std::ifstream:

• std::ifstream constructor
• C++ Basic file I/O

Also remember that, once you have a file open, std::ifstream works pretty much exactly like std::cin, which you should be familiar with. The main difference is that instead of reading input from the user in the terminal, std::ifstreamreads input from a file.