Friday, September 19, 2008

C program to print count of lines, words and characters in a text file

C program to print count of lines, words and characters in a text file

#include< stdio.h >
#include< stdlib.h >

void main(int argc,char argv[])
{
int char_count=0,line_count=0,word_count=0;
FILE *fp;
char ch;

//char w='\n';
//sprintf("%d",w);
//if(argc!=1)
// printf("One argument is needed\n");
//fp=fopen("argv[1]","r");

fp=fopen("c:\\kk.txt","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
{
break;
}
char_count++;
if(ch==' ')
word_count++;
if(ch=='\n')
{
line_count++;
word_count++;
}
}
printf("word count =%d\n Line count =%d\n char countd\n",word_count,line_count,char_count);

}

0 comments: