Skip to main content

Featured

functions in Python

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:

 

Comments

  1. 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

Post a Comment

Popular Posts