Featured
- Get link
- X
- Other Apps
C++ program to count vowels and consonants in text file
/*Following program counts the vowels and consonants from a text file Sample.txt */
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
fstream f;
f.open("Sample.txt",ios::out);
f<<"This is a test to SEE";
f.close();
f.open("sample.txt",ios::in);
int vowels=0,cons=0,space=0;
char ch;
f.get(ch);
while(f)
{
if(ch>='a' && ch<='z')
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vowels++;
else
cons++;
}
if(ch>='A' && ch<='Z')
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vowels++;
else
cons++;
}
else if(ch==' ')
space++;
f.get(ch);
}
f.close();
cout<<"\n Vowels Are : "<<vowels;
cout<<"\n Consonants Are : "<<cons;
cout<<"\n Spaces Are : "<<space;
getch();
}
OUTPUT:
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
fstream f;
f.open("Sample.txt",ios::out);
f<<"This is a test to SEE";
f.close();
f.open("sample.txt",ios::in);
int vowels=0,cons=0,space=0;
char ch;
f.get(ch);
while(f)
{
if(ch>='a' && ch<='z')
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vowels++;
else
cons++;
}
if(ch>='A' && ch<='Z')
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vowels++;
else
cons++;
}
else if(ch==' ')
space++;
f.get(ch);
}
f.close();
cout<<"\n Vowels Are : "<<vowels;
cout<<"\n Consonants Are : "<<cons;
cout<<"\n Spaces Are : "<<space;
getch();
}
OUTPUT:
- Get link
- X
- Other Apps
Labels:
C++ program to count vowels and consonants in text file
file handling program in C++ to count vowels and consonants
Comments
Popular Posts
C++ program to display numbers which get reversed after multiplying by 4
- Get link
- X
- Other Apps
In f.open("sample.txt",ios::in) there is error it should be f.open("Sample.txt",ios::in) then you will get the output
ReplyDelete