C program to count number of characters, words & lines of a text file.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
FILE *fp;
char ch;
int cc,wc,lc;
clrscr();
fp=fopen ("abc.txt","r+");
if(fp==NULL){
printf("can not open a file\n");
getch();
exit(0);
}
while (ch != EOF){
ch=fgetc(fp);
printf("%c",ch);
if (ch==' ',ch) {wc++;}
if (ch=='\n') {lc++;wc++;}
cc++;
}
printf("\n Number of character = %d\n, Number of words=%d\n, Number of lines=%d\n",cc,wc,lc);
getch();
fclose(fp);
}

First create a text file like "abc.txt", write contents of it and save it into relative folder like BIN then run this program

No comments:

Post a Comment