In: Computer Science
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file.
The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the is_functions in ctype.h
int nletters = 0;
int nlower = 0;
int nupper = 0;
int ndigits = 0;
int npuncts = 0;
int nspaces = 0;
int nalnum = 0;//digit or letter
int nlines = 0; //count end of line chars '\n' or EOF
Code:
//including libraries required
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
//file pointers
FILE *fptr1, *fptr2;
//string to store file name
//and character to read each character from file
char filename[20], c;
//count to store number of each character
int nletters = 0;
int nlower = 0;
int nupper = 0;
int ndigits = 0;
int npuncts = 0;
int nspaces = 0;
int nalnum = 0;
int nlines = 0;
//taking input from the user name of the file
printf("Enter the filename to open for reading
\n");
scanf("%s", filename);
// opening file
fptr1 = fopen(filename, "r");
//if file is empty
if (fptr1 == NULL)
{
printf("Cannot open file
%s \n", filename);
exit(0);
}
//reading each character from file
c = fgetc(fptr1);
//this loop runs until file reaches the
end
while (c != EOF)
{
//increasing count for every
corresponding character
if(isalpha(c)){
nletters++;
}
if(isalnum(c)){
nalnum++;
}
if(isdigit(c)){
ndigits++;
}
if(islower(c)){
nlower++;
}
if(isupper(c)){
nupper++;
}
if(isspace(c)){
nspaces++;
}
if(ispunct(c)){
npuncts++;
}
if(c=='\n'){
nlines++;
}
//reading next
character
c = fgetc(fptr1);
}
// increasing count for EOF
nlines++;
//closing the file
fclose(fptr1);
//opening new file for writing
fptr2 = fopen("out.txt", "w");
// printing to both file and screen
fprintf(fptr2,"The number of
Alphabets are : %d\n",nletters);
fprintf(fptr2,"The number of
Numbers are : %d\n",ndigits);
fprintf(fptr2,"The number of spaces
are : %d\n",nspaces);
fprintf(fptr2,"The number of
uppercase letters are : %d\n",nupper);
fprintf(fptr2,"The number of
lowercase letters are : %d\n",nlower);
fprintf(fptr2,"The number of alnums
are : %d\n",nalnum);
fprintf(fptr2,"The number of
newlines are : %d\n",nlines);
fprintf(fptr2,"The number of
Punctuations are : %d\n",npuncts);
printf("The number of Alphabets are
: %d\n",nletters);
printf("The number of Numbers are :
%d\n",ndigits);
printf("The number of spaces are :
%d\n",nspaces);
printf("The number of uppercase
letters are : %d\n",nupper);
printf("The number of lowercase
letters are : %d\n",nlower);
printf("The number of alnums are :
%d\n",nalnum);
printf("The number of newlines are
: %d\n",nlines);
printf("The number of Punctuations
are : %d\n",npuncts);
fclose(fptr2);
//closing file
return 0;
}
Output:
File that we are going to read from:
Output:
File we wrote to:
Code Screenshot:
Code Snippet:
//including libraries required
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
//file pointers
FILE *fptr1, *fptr2;
//string to store file name
//and character to read each character from file
char filename[20], c;
//count to store number of each character
int nletters = 0;
int nlower = 0;
int nupper = 0;
int ndigits = 0;
int npuncts = 0;
int nspaces = 0;
int nalnum = 0;
int nlines = 0;
//taking input from the user name of the file
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// opening file
fptr1 = fopen(filename, "r");
//if file is empty
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
//reading each character from file
c = fgetc(fptr1);
//this loop runs until file reaches the end
while (c != EOF)
{
//increasing count for every corresponding character
if(isalpha(c)){
nletters++;
}
if(isalnum(c)){
nalnum++;
}
if(isdigit(c)){
ndigits++;
}
if(islower(c)){
nlower++;
}
if(isupper(c)){
nupper++;
}
if(isspace(c)){
nspaces++;
}
if(ispunct(c)){
npuncts++;
}
if(c=='\n'){
nlines++;
}
//reading next character
c = fgetc(fptr1);
}
// increasing count for EOF
nlines++;
//closing the file
fclose(fptr1);
//opening new file for writing
fptr2 = fopen("out.txt", "w");
// printing to both file and screen
fprintf(fptr2,"The number of Alphabets are : %d\n",nletters);
fprintf(fptr2,"The number of Numbers are : %d\n",ndigits);
fprintf(fptr2,"The number of spaces are : %d\n",nspaces);
fprintf(fptr2,"The number of uppercase letters are : %d\n",nupper);
fprintf(fptr2,"The number of lowercase letters are : %d\n",nlower);
fprintf(fptr2,"The number of alnums are : %d\n",nalnum);
fprintf(fptr2,"The number of newlines are : %d\n",nlines);
fprintf(fptr2,"The number of Punctuations are : %d\n",npuncts);
printf("The number of Alphabets are : %d\n",nletters);
printf("The number of Numbers are : %d\n",ndigits);
printf("The number of spaces are : %d\n",nspaces);
printf("The number of uppercase letters are : %d\n",nupper);
printf("The number of lowercase letters are : %d\n",nlower);
printf("The number of alnums are : %d\n",nalnum);
printf("The number of newlines are : %d\n",nlines);
printf("The number of Punctuations are : %d\n",npuncts);
fclose(fptr2);
//closing file
return 0;
}