C Programming Tutorial

Advertisements

Write C program to count total number of consonants in a file



       #include<stdio.h>
       #include<conio.h>
       #include<process.h>

       void main()
       {
              FILE *fp;
              char ch;
              int count=0;
              clrscr();

              fp = fopen("file.txt","r");
              
              if(fp == NULL)
              {
                     printf("\nCan't open file or file doesn't exist."); 
                     exit(0);
              }

              while((ch = fgetc(fp))!=EOF)
                  if((ch>='A') && (ch<='Z') || (ch>='a') && (ch<='z'))
                  {
                      if(ch!='a' && ch!='e' && ch!='i' && ch!='o' && ch!='u' && ch!='A' && ch!='E' && ch!='I' && ch!='O' && ch!='U')
                              count++;
                   }                   
              printf("\nTotal consonants : %d",count);          
              fclose(fp);               

              getch();
       }




Advertisement