Write a program to count the number of characters and words in a given file. 10m Jun2007

By | July 4, 2014

Write a program to count the number of characters and words in a given file. 10m Jun2007 

#include<stdio.h>
#include<process.h>
void main()
{
FILE *fp;
int chr=0,wrd=1,str;
clrscr();
if((fp=fopen(“test.txt”,”r”))== NULL)
{
printf(“File does not exist\n”);
exit(0);
}
while(!(feof(fp)))
{
str=fgetc(fp);
if(str==’ ‘)
wrd++;
chr++;
}
printf(“The number of characters in file :%d\n”,chr);
printf(“The number of words in the file :%d\n”,wrd);
fclose(fp);
getch();
}

Code:

#include<stdio.h>
#include<process.h>
void main()
{
FILE *fp;
int chr=0,wrd=1,str;
clrscr();
if((fp=fopen("test.txt","r"))== NULL)
{
printf("File does not exist\n");
exit(0);
}
while(!(feof(fp)))
{
str=fgetc(fp);
if(str==' ')
wrd++;
chr++;
}
printf("The number of characters in file :%d\n",chr);
printf("The number of words in the file :%d\n",wrd);
fclose(fp);
getch();
}

Screen Shots:

File_Chars_Words

 File_Chars_Words_Output

 

 Write a program to display the string “EARTH” in the following format: 10m Jun2007

E

E A

E A R

E A R T

E A R T H

E A R T

E A R

E A

E

 Just Replace Array from “IGNOU” to “EARTH”

Solved program can be found on this link http://cssimplified.com/c-programming/write-a-program-in-c-to-print-the-following-format-10m-dec2005

Leave a Reply