Site icon DataFlair

C Program to Count Number of Character, Space and Newlines in File

Program 1

//Program for count space and character from file also lines 
#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *fp;
   int sp=0,cc=0,nl=0;
   char ch;
   fp=fopen("e://cdata/employee.txt","r");
   if(fp==NULL)
   printf("File not found....");
   else
   {
       do
       {
           ch=fgetc(fp);
           if(ch==' ')
             sp++;
           if(ch=='\n')  
            nl++;
           else
            cc++;  
       }while(ch!=EOF);
       printf("\nTotal character is %d",cc);
       printf("\nTotal space  is %d",sp);
       printf("\nTotal lines  is %d",nl);
   }
}

 

Exit mobile version