Monday, June 18, 2012

To count characters, spaces, tabs and newlines in a file


/* Program to count characters, spaces, tabs and newlines in a file */

#include<stdio.h>

#include<conio.h>

void main()

{

  FILE *fp;

  char ch;

  int nol=0,not=0,nob=0,noc=0;

  clrscr();

  fp=fopen("h04.c","r");

  while(1)

  {

    ch=fgetc(fp);

    if(ch==' ')

      nob++;

    else if(ch=='\n')

      nol++;

    else if(ch=='\t')

      not++;

    else if(ch==EOF)

      break;

    else

      noc++;

  }

  fclose(fp);

  printf("\nNumber of characters=%d",noc);

  printf("\nNumber of blanks=%d",nob);

  printf("\nNumber of tabs=%d",not);

  printf("\nNumber of lines=%d",nol);

  getch();

}

 

OUTPUT

Number of characters=375

Number of blanks=90

Number of tabs=0

Number of lines=25

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.