Monday 31 August 2015

C Program to count characters, lines, spaces & tabs in a file

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fp;
 char a[20];
 int nol=0,not=0,nob=0,noc=0;
 char c;
 clrscr();
 printf("Enter the name of File:\n");
 gets(a);
 if((fp=fopen(a,"r"))==NULL)
 {
  printf("File dosen't exist.");
 }
 else
 {
  while(1)
  {
   c=fgetc(fp);
   if(c==EOF)
      break;
   noc++;
   if(c==' ')
   nob++;
   if(c=='\n')
   nol++;
   if(c=='\t')
   not++;
  }
 }
 fclose(fp);
 printf("Number of characters = %d\n",noc);
 printf("Number of blanks = %d\n",nob);
 printf("Number of tabs = %d\n",not);
 printf("Number of lines = %d\n",nol);
 getch();
}

Output

Leave a Reply

 
 

Labels

Blog Archive