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

Create a new project called FileStats Copy the contents of main.c from CharHisto

ID: 3757259 • Letter: C

Question

Create a new project called FileStats

Copy the contents of main.c from CharHistogram prject (you can use mine) and modify the code to do the following:

Declare thse variables and initialize them to 0:

int letters = 0;

int digits = 0;

int puncts = 0;

int whites = 0;

int upper = 0;

int lower = 0;

int alnum = 0;//digit or letter

int words = 0; //check if current char is not space and prev char is space

int lines = 0; //count end of line chars ' ' or EOF

Calculate the above variables (letters, digits, ...).

Print to screen the above variables, one in each line.

Print to the output file the above variables, one in each line.

Make use of the is_ functions in C: https://www.tutorialspoint.com/c_standard_library/ctype_h.htm such as:

isdigit(ch)

isletter(ch)

int pCh = ' '; //to count words

int ch = getc(fin);

while (ch != EOF) //feof(FileIn)

{

hist[ch]++;

if (isdigit(ch))

{

digits++;

}

//your calculations go here

if (!isspace(ch) && isspace(pCh))

{

words++;

}

pCh = ch;

ch = getc(fin); //read a new char

}

CharHistogram Program:

#pragma warning( disable : 4996 )

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

enum { MaxLine = 132, MaxChars = 128 };

int main(void)

{

   FILE *fin = NULL;

   FILE *fout = NULL;

   char Line[MaxLine];

   int hist[MaxChars];

   while (1)

   {

       puts("Enter input text file name, any char to quit");

       gets(Line);

       if (strlen(Line) <= 1) //strln = string length in string.h

           break;

       fin = fopen(Line, "r");

       if (!fin)

       {

           puts("Failed to open input file");

           continue; //ignore this file and go to next;

       }

       puts("Enter output text file name, any char to quit");

       gets(Line);

       fout = fopen(Line, "w");

       if (!fout)

       {

           puts("Failed to open output file");

           continue; //ignore this file and go to next;

       }

       //initialize histogram conents to 0

       for (int index = 0; index < MaxChars; index++)

           hist[index] = 0;

       int ch = getc(fin);

       while (ch != EOF) //feof(FileIn)

       {

           hist[ch]++;

           ch = getc(fin); //read a new char

       }

       for (int index = 32; index < 127; index++)

       {

           fprintf(fout, "%d: %c = %d ", index, index, hist[index]);

           printf("%d: %c = %d ", index, index, hist[index]);

       }

       fclose(fin);

       fclose(fout);

   }

   getchar();

   return 0;

}

Explanation / Answer

If you have any doubts, please give me comment...

#pragma warning(disable : 4996)

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <stdlib.h>

#include <ctype.h>

enum

{

MaxLine = 132,

MaxChars = 128

};

int main(void)

{

FILE *fin = NULL;

FILE *fout = NULL;

char Line[MaxLine];

int hist[MaxChars];

int letters = 0;

int digits = 0;

int puncts = 0;

int whites = 0;

int upper = 0;

int lower = 0;

int alnum = 0; //digit or letter

int words = 0; //check if current char is not space and prev char is space

int lines = 0; //count end of line chars ' ' or EOF

while (1)

{

puts("Enter input text file name, any char to quit");

gets(Line);

if (strlen(Line) <= 1) //strln = string length in string.h

break;

fin = fopen(Line, "r");

if (!fin)

{

puts("Failed to open input file");

continue; //ignore this file and go to next;

}

puts("Enter output text file name, any char to quit");

gets(Line);

fout = fopen(Line, "w");

if (!fout)

{

puts("Failed to open output file");

continue; //ignore this file and go to next;

}

//initialize histogram conents to 0

for (int index = 0; index < MaxChars; index++)

hist[index] = 0;

int ch = getc(fin);

char pCh = '';

while (ch != EOF) //feof(FileIn)

{

hist[ch]++;

if(isdigit(ch))

digits++;

if(isalpha(ch))

letters++;

if(!isspace(ch) && isspace(pCh))

words++;

if(ispunct(ch))

puncts++;

if(isspace(ch))

whites++;

if(isupper(ch))

upper++;

if(islower(ch))

lower++;

if(isalnum(ch))

alnum++;

if(ch==' ')

lines++;

pCh = ch;

ch = getc(fin); //read a new char

}

lines++;

for (int index = 32; index < 127; index++)

{

fprintf(fout, "%d: %c = %d ", index, index, hist[index]);

printf("%d: %c = %d ", index, index, hist[index]);

}

printf("letters = %d ", letters);

printf("digits = %d ", digits);

printf("puncts = %d ", puncts);

printf("whites = %d ", whites);

printf("upper = %d ", upper);

printf("lower = %d ", lower);

printf("alnum = %d ", alnum);

printf("words = %d ", words);

printf("lines = %d ", lines);

fprintf(fout, "letters = %d ", letters);

fprintf(fout, "digits = %d ", digits);

fprintf(fout, "puncts = %d ", puncts);

fprintf(fout, "whites = %d ", whites);

fprintf(fout, "upper = %d ", upper);

fprintf(fout, "lower = %d ", lower);

fprintf(fout, "alnum = %d ", alnum);

fprintf(fout, "words = %d ", words);

fprintf(fout, "lines = %d ", lines);

fclose(fin);

fclose(fout);

}

getchar();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote