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

Help use any random text file just needs to count each letter speratly and store

ID: 3877875 • Letter: H

Question

Help use any random text file just needs to count each letter speratly and store in an array and court each occurance Step 2: Write a program to count letters in a file Your git repo for this lab will come with a file called testfile.txt. Take a look at this file. 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 (le. 'A' and'a count the same) and gnoring punctuation or other non-letter characters. That means there are 26 characters I 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 aftreaOnce you have the file opened as an ifstream, you should pass the ifatream to a function like this void count_lettezs(ifstreams, ne This function should do the work or actually reading through the fstream 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 impiement it in such a way that you only have to make one pass reading through the fie and dont have to allocate any additional memory (beyond the sfatrean and the array of ants) See if you can figure out how to do that Ater you've worked on this problem for a littie nt your approaches to go over

Explanation / Answer

Here i am using g++ compiler. I think this code will work in other compilers as well.

File: count_letters.cpp

#include <iostream>
#include <fstream>
using namespace std;

void count_letters(ifstream &, int []);    //Function Declaration   

int main(int argc, char * argv[])
{

   char c1, c2, ch1 = 'A';
   int arr[26] = {0};                        //Initializing array with dummy values( 0 )
   ifstream fin(argv[1], ios::in);        //Opening file in read mode
   if(!fin.is_open())                         //Checking whether the file is opened or not
   {
       cout<<"Cannot open file ";
       return 1;
   }
   cout<<"File opened successfully ";
   count_letters(fin, arr);             //Calling count_letters function
   for(int i = 0; i< 26; i++)          //Displaying the character wise count
   {
       c1 = ch1+i;
       c2 = c1+32;
       cout<<c1<<" and "<<c2<<" letters in file "<<argv[1]<<": "<<arr[i]<<" ";
   }
   return 0;          /* End of main */
}

inline void count_letters(ifstream& fi, int a[])
{
   char ch;
   int i, j;
   while((ch = fi.get()) != EOF)       //Reading one character from file using ifstream
   {
       for(j = 0; j< 26; j++)             //Loop will repeat from A-Z and a-Z
       {
           if(ch == (65+j) || ch == (97+j))   //Comparing with letter
           {
               a[j]++;                                //Incrementing the corresponding character count
               break;
           }
       }
   }
}

Output:

% cat testfile.txt            #contents of testfile.txt

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz.
This is line2
Hello Good morning
How are you?

% g++ -o count_letters count_letters.cpp

% count_letters testfile.txt

File opened successfully
A and a letters in file testfile.txt: 3
B and b letters in file testfile.txt: 2
C and c letters in file testfile.txt: 2
D and d letters in file testfile.txt: 3
E and e letters in file testfile.txt: 5
F and f letters in file testfile.txt: 2
G and g letters in file testfile.txt: 4
H and h letters in file testfile.txt: 5
I and i letters in file testfile.txt: 6
J and j letters in file testfile.txt: 2
K and k letters in file testfile.txt: 2
L and l letters in file testfile.txt: 5
M and m letters in file testfile.txt: 3
N and n letters in file testfile.txt: 5
O and o letters in file testfile.txt: 8
P and p letters in file testfile.txt: 2
Q and q letters in file testfile.txt: 2
R and r letters in file testfile.txt: 4
S and s letters in file testfile.txt: 4
T and t letters in file testfile.txt: 3
U and u letters in file testfile.txt: 3
V and v letters in file testfile.txt: 2
W and w letters in file testfile.txt: 3
X and x letters in file testfile.txt: 2
Y and y letters in file testfile.txt: 3
Z and z letters in file testfile.txt: 2

Hoping you will understand. In case of any doubts comment it below.

Thank you...