Write a C program to count blanks, digits, letters, newlines and others from the source file.

#include<stdio.h>
#include<math.h>
int main()
{
   int d,blank,digit,leter,newLine,others;
   char ch;
   blank=0;
   leter=0;
   digit=0;
   newLine=0;
   others=0;
   d=0;
   FILE *fp;
   fp=fopen("ex.c","r");
   if(fp==NULL)
   {
       printf("\n file unable to create : 1.c");
       exit(1);
   }
   while((ch=fgetc(fp))!=EOF)
   {
       if(ch==' ')
//        {
            blank++;
        }
        else if(ch>='0' && ch<='9')
        {
            digit++;
        }
        else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
         {
             leter++;
         }
                    else if (ch == '\n')
                    {
                       newLine++;

                     }
                     else others++;

     }

     fclose(fp);
      printf("\nblancks  = %d",blank);
      printf("\ndigit    = %d",digit);
      printf("\nletters  = %d",leter);
      printf("\nnewlines = %d",newLine);
      printf("\nothers   = %d",others);

}

Write a C program to count blanks, digits, letters, newlines and others from the source file. Write a C program to count blanks, digits, letters, newlines and others  from the source file. Reviewed by Unknown on 03:28 Rating: 5

No comments:

Powered by Blogger.