Write a function in a C++ to count the number of vowels, consonants, digits and special characters present in a dynamically selected text file.

void main()
{
system("cls");
char filename[20];
system("dir/w *.txt");

cout<<"Enter the file name you want to read: ";
cin>>filename;

ifstream fin(filename,ios::in);
char ch;
int vow=0, sp=0, cons=0, digit=0,total=0;
while(!fin.eof())
{
fin.get(ch);
if (isalpha(ch))
{
   switch(ch)
   {
case 'a': case 'A': case 'e': case 'E': case 'o':
case 'O': case 'u': case 'U': case 'i': case 'I':
vow++;
break;

default:
cons++;
   }
}
else if(isdigit(ch))
digit++;
else
sp++;
total++;
}
fin.close();
cout<<"Total Characters in file are : "<<total<<endl;
cout<<"Total Vowels in file are     : "<<vow<<endl;
cout<<"Total Consonants in file are : "<<cons<<endl;
cout<<"Total Digits in files are    : "<<digit<<endl;
system("pause");
}






No comments

Post your comments

Powered by Blogger.