Write a program to count number of lines, words and characters in a file.
CODING:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount,wordcount,charcount;
linecount=0;
wordcount=0;
charcount=0;
clrscr();
printf("Enter A Filename:");
gets(filename);
fp=fopen(filename,"r");
if(fp)
{
while ((ch=getc(fp)) !=EOF)
{
if(ch !=' ' && ch !='\n')
{++charcount;}
if(ch==' ' || ch=='\n')
{++wordcount;}
if(ch=='\n')
{
++linecount;
}
}
if(charcount>0)
{++linecount;
++wordcount;
}
}
else
{
printf("failed to open the file\n");
}
printf("lines:%d\n",linecount);
printf("words:%d\n",wordcount);
printf("characters:%d\n",charcount);
getch();
return(0);
}
CODING:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount,wordcount,charcount;
linecount=0;
wordcount=0;
charcount=0;
clrscr();
printf("Enter A Filename:");
gets(filename);
fp=fopen(filename,"r");
if(fp)
{
while ((ch=getc(fp)) !=EOF)
{
if(ch !=' ' && ch !='\n')
{++charcount;}
if(ch==' ' || ch=='\n')
{++wordcount;}
if(ch=='\n')
{
++linecount;
}
}
if(charcount>0)
{++linecount;
++wordcount;
}
}
else
{
printf("failed to open the file\n");
}
printf("lines:%d\n",linecount);
printf("words:%d\n",wordcount);
printf("characters:%d\n",charcount);
getch();
return(0);
}
Comments
Post a Comment