In: Computer Science
C++ Program:
/* C++ Program that counts number of vowels in the file */
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Main function
int main()
{
char ch;
string fileName;
int vowelCount=0, aCnt=0, eCnt=0, iCnt=0,
oCnt=0, uCnt=0;
//Reading file name
cout << "\n Input file name: ";
cin >> fileName;
//Opening file in read mode
fstream fin(fileName, ios::in);
//Checking for file existence
if(fin.fail())
{
//Printing error
message
cout << "\n
Error!!! Cannot read input file... \n";
return -1;
}
//Fetching data from file
while(fin.good())
{
//Reading character from
file
fin >> ch;
//Checking for
character
switch(ch)
{
//Vowel 'a'
case 'a':
case 'A': aCnt++; vowelCount++; break;
//Vowel 'e'
case 'e':
case 'E': eCnt++; vowelCount++; break;
//Vowel 'i'
case 'i':
case 'I': iCnt++; vowelCount++; break;
//Vowel 'o'
case 'o':
case 'O': oCnt++; vowelCount++; break;
//Vowel 'u'
case 'u':
case 'U': uCnt++; vowelCount++; break;
default: break;
}
}
//Printing final report
cout << "\n\n Total number of Vowels: "
<< vowelCount;
cout << "\n\n Vowel 'A': " <<
aCnt;
cout << "\n\n Vowel 'E': " <<
eCnt;
cout << "\n\n Vowel 'I': " <<
iCnt;
cout << "\n\n Vowel 'O': " <<
oCnt;
cout << "\n\n Vowel 'U': " <<
uCnt;
cout << "\n\n";
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: