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

Project 2 Description: In this project, you are asked to write a program which c

ID: 3820372 • Letter: P

Question

Project 2

Description:
In this project, you are asked to write a program which can open a text file, read its content and count the number of each alphabetic character regardless of its case. It means that program needs to count for example how many letter of ‘A’ or ‘a’ are in the text file. The counting is for all alphabetic characters; thus, all other characters such as numbers and punctuation characters should be excluded. The output of the program should be formatted in two columns as:
A                    #
B                     #
C                     #
….
Z                     #
Note: for now you can write the program and test it on any text file and my suggestion is that you create your own text file, where you can have some pre-determine number of characters. For Submission, Should run the program using the source code of project #1 that I gave belove.

Sourse code:

/*
                    Projects #1
      Adding the line number to a text file

*/

#include <stdio.h>

void main(){

   int j=0,i=0;
   FILE *in,*out;

   in=fopen("c://test_c/BMI.c","r");
   out=fopen("c://test_c/BMI_n.c","w");
   char o[100]={''};

   do {
      o[i++]=(fgetc(in));
      if (o[i-1]==' ' )
       {
           o[i]='';
           fprintf(out,"%-.3d: %s",++j,o);
            i=0;
        }

         } while (o[i-1]!=EOF);

     fclose(in);
     fclose(out);
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
int *count = (int *)calloc(26,sizeof(int)) ;  
FILE *f;
FILE *fw;
int i;
char ch;
f = fopen("C:/Users/aditya/Desktop/input.txt","r");
fw = fopen("C:/Users/aditya/Desktop/charFreq.txt","w");
while((ch =fgetc(f)) != EOF)
{
   if (tolower(ch) >= 'a' && tolower(ch) <='z') {
       char temp = tolower(ch);
       count[temp - 'a' ]++;
   }
}
for(i = 0;i < 26;i++)
fprintf(fw,"%c %d ",65+i,count[i]);
fclose(f);
fclose(fw);
return 0;
}