Sunday, April 24, 2016

c program to count number of alphabets, digits and spaces in a given file

#include<stdio.h>
//included process.h for using the function exit(0)
#include<process.h>
void main()
{
 FILE *fp;
 char c;
//a will stand for number of alphabets
//d for digits
//s for spaces
 int a=0,d=0,s=0;
//this is the name of file to read
 fp=fopen("emp1.txt","r");
 if(fp==NULL)
  {
   printf("Not open");
//it will exit the program
   exit(0);
  }
 while((c=fgetc(fp))!=EOF)
  {
//we are recognizing alphabets based on thier ascii codes
   if((c>=65&&c<=90)||(c>=97&&c<=122))
    a++;
   else if(c>=48&&c<=58)
    d++;
   else
    s++;
  }
 printf("alphabets=%d,digits= %d ,spaces=%d",a,d,s);
}

output
alphabets=2, digits =5, spaces=8

No comments:

Post a Comment