Write program in c++ Assume letters A, E, I, O, and U as the vowels. Write a pro
ID: 3681841 • Letter: W
Question
Write program in c++
Assume letters A, E, I, O, and U as the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop:
Read the one line from the input file and store it in a string;
Count the number of vowels and consonants (using either while-loop or for-loop) in the string The while-loop will terminate when the end-of-file is reached. After the loop is finished, display the total number of vowels and consonants in the text file. [A text file, named “ass4_Q1_input.txt”, is provided as your testing input file.]
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
// function to check whether a character is vowel or not
bool isVowel(char c){
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U') {
return true;
}
else {
return false;
}
}
// function to check whether a character is consonant or not
bool isConsonant(char c){
if(!isVowel(c) && ((c>='a' && c<='z')||(c>='A' && c<='Z')))
return true;
else
return false;
}
int main(){
ifstream infile("ass4_Q1_input.txt");
int vowelCount=0, consCount=0;
string line;
while (getline(infile, line)) // reading line by line
{
unsigned int i=0;
while(i < line.size()){ // traversing current line
if(isVowel(line.at(i)))
vowelCount++;
else if(isConsonant(line.at(i)))
consCount++;
i++;
}
}
cout<<"Total number of vowel: "<<vowelCount<<endl;
cout<<"Total number of consonaant : "<<consCount<<endl;
return 0;
}
/*
Sample Run:
Content of ass4_Q1_input.txt:
Read the one line from the input file and store it in a string;
Count the number of vowels and consonants (using either while-loop or for-loop)
in the string The while-loop will terminate when the end-of-file is reached.
After the loop is finished, display the total
Output:
Total number of vowel: 82
Total number of consonaant : 129
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.