C program to count the number of characters, words and lines in a file
#include<stdio.h> #include<conio.h> void main()
{
int noc=0,now=0,nol=0; FILE *fw,*fr;
char fname[20],ch; clrscr();
printf("Enter the source file name:"); gets(fname);
fr=fopen(fname,"r"); if(fr==NULL)
{
printf("\n error \n"); exit(0);
}
do
{
ch=fgetc(fr); noc++; if(ch==' ')
now=1+now; if(ch=='\n')
{
nol++; now++;
}
} while(ch!=EOF); fclose(fr);
printf("\n Total no of character = %d",noc); printf("\n Total no of words = %d",now+1); printf("\n Total no of lines = %d",nol+1); getch();
}
Output
Enter the source file name: realloc.c Total no of character = 431
Total no of words = 50 Total no of lines = 25
Comments
Post a Comment