C program to count the number of vowels in a given file
In this article section or program we will learn and demonstrate a simple C program to count the number of vowels in a given file , the file name will be given as command line argument to the program.
we all know that ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters.C programming Language is case sensitive, and hence uppercase and lowercase characters are considered as unique or different, so we need to check for both the cases.
Consider file sample.txt with the following content
I love c Language.
The C program to count number of vowels in a file.
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
FILE *fptr;
int vowels=0,consonants=0;
char c;
if(argc!=2)
{
printf("Not sufficient Arguments Given Try Again");
exit(0);
}
fptr1=fopen(argv[1],"r");
if(fptr1==NULL)
{
printf("OOPs! Source can't be opened");
exit(0);
}
c=fgetc(fptr1);
while(c!=EOF)
{
if((c=='A')||(c=='a')||(c=='e')||(c=='E')||(c=='I')||(c=='i')||(c=='o') ||(c=='O')||(c=='U')||(c=='u'))
{
vowels++;
}
else
{
consonants++;
}
c=fgetc(fptr1);
}
printf("\n Number of vowels are = %d",vowels);
printf("\n Number of consonants are = %d", consonants);
return(0);
}
return 0;
}
our file name is demo.c
Output
demo.exe sample.txt
Number of vowels are =7
Number of consonants are =8
The program given above, Counts the number of vowels and consonents in a given file entered by the user .
Previous Topic:-->> Update file contents in C. || Next topic:-->>Micro substitution in c.