**NOTE: The program should be written in (C programming language), please show t
ID: 3837356 • Letter: #
Question
**NOTE: The program should be written in (C programming language), please show the result for good rate, The code that you need to edit will be under the picture.
THE CODE THAT NEED TO BE ADITED:
#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);
}
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: 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 l posted on iLearn.Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_CHARS 255
typedef unsigned char byte;
void init_histogram(int histo[]);
void cons_histogram(FILE* file, int histo[]);
void display_histogram(int histo[]);
void write_histogram(int histo[], FILE *out);
int main(int argc, char const *argv[])
{
if(argc < 2){
printf("Please pass file name ");
return 1;
}
FILE *in,*out;
in=fopen("c://test_c/BMI.c","r");
out=fopen("c://test_c/BMI_n.c","w");
if(in == NULL){
printf("Error in opening input file ");
exit(0);
}
if(out == NULL){
printf("Error in opening output file ");
exit(0);
}
int histo[MAX_CHARS];
init_histogram(histo);
cons_histogram(in, histo);
write_histogram(histo, out);
fclose(in);
fclose(out);
display_histogram(histo);
printf("Output file written successfully ");
return 0;
}
void init_histogram(int histo[]){
int i;
for(i=0; i<MAX_CHARS; i++)
histo[i] = 0;
}
void cons_histogram(FILE* file, int histo[]){
char c;
int index;
c = (char)fgetc(file);
while(c != EOF){
if(isalpha(c)){
index = (int)c;
histo[index]++; // increasign count
}
c = (char)fgetc(file);
}
}
void display_histogram(int histo[]){
int i;
//printf("Char Count ");
for(int i=0; i<MAX_CHARS; i++){
if(histo[i] > 0){
char c = (byte)i;
printf("%c %d ", c, histo[i]);
//printf("%c %d ", c, histo[i]);
}
}
}
void write_histogram(int histo[], FILE *out){
int i;
//printf("Char Count ");
for(int i=0; i<MAX_CHARS; i++){
if(histo[i] > 0){
char c = (byte)i;
fprintf(out, "%c %d ", c, histo[i]);
//printf("%c %d ", c, histo[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.