Count spaces, vowels, consonants and digits in a file

This is a C program to count spaces ,vowels, consonants, digits in a file and write it at te end of the file.

You can compile this program on bOtskOOl Free Online C/C++ Compiler

 

SOURCE CODE

/*
A C Example to count spaces ,vowels, consonants, digits in a file and write it
 at end of the file
*/
 
/*
**********************
www.botskool.com
**********************
*/
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>
 
read_file(char*);
 
void main()
{
char ch,*filename="file.txt";
FILE *fpt;
int space,digit,vow,conso;
clrscr();
 
fpt=fopen(filename,"a+");
if(!fpt)
 {
 printf("\n\"%s\" Not Found . terminating......",filename);
 exit(0);
 }
space=digit=conso=vow=0;
while((ch=fgetc(fpt))!=EOF)
{
 
if(isspace(ch))
space++;
 
if(isdigit(ch))
digit++;
 
if(isalpha(ch))
   { switch(tolower(ch))
                {
                                case 'a' : vow++;
                                                    break;
                                case 'e' : vow++;
                                                    break;
                                case 'i' : vow++;
                                                    break;
                                case 'o' : vow++;
                                                    break;
                                case 'u' : vow++;
                                                    break;
 
                       default    : conso++;
                                                    break;
                }
   }
}
 
fflush(fpt);
 
fprintf(fpt,"\nNo of vowels :   %d",vow);
fprintf(fpt,"\nNo of consonants : %d",conso);
fprintf (fpt,"\nNo of digits : %d",digit);
fprintf(fpt,"\nno of white spaces : %d",space);
fclose(fpt);
 
printf("\n FILENAME : \"%s\"",filename);
read_file(filename);
 
getch();
 
 
}
 
read_file(char *filename)
{
char ch;
FILE *read=fopen(filename,"r");
 
if(!read)
 {
 printf("\n \"%s\" file could not be read!!---\n",filename);
 getch();
 exit(0);
 }
 
 
printf("\nCONTENTS OF \"%s\"\n",filename);
while(1)
{
ch=fgetc(read);
if(ch!=EOF)
 printf("%c",ch);
else
 break;
}
fclose(read);
//getch();
}
 
/*
**********************
www.botskool.com
**********************
*/

 

Output of the above program is shown below-

Output

 

Download source code.

 

 

Terms of Agreement:  
By using this code, you agree to the following terms-  
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.  
2) You MAY NOT redistribute this code (for example to a web site) without written permission from us. Failure to do so is a violation of copyright laws.  
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which may have placed in the code or code's description.

 >>Kindly post your doubts and suggestions on our discussion forum.

Tags: