#include <iostream> #include <string> using namespace std; void count(string,int
ID: 3624783 • Letter: #
Question
#include <iostream>#include <string>
using namespace std;
void count(string,int&,int&,int&,int&,int&);
int main()
{
string sentence;
int i=0;
int vow_a=0, vow_e=0,vow_i=0, vow_o=0, vow_u=0;
cout<<"Please enter a sentence. ";
getline(cin,sentence);
count(sentence,vow_a,vow_e,vow_i, vow_o, vow_u);
cout<<"The number of 'a' is "<< vow_a<<endl;
cout<<"The number of 'e' is "<< vow_e<<endl;
cout<<"The number of 'i'is "<< vow_i<<endl;
cout<<"The number of 'o'is "<< vow_o<<endl;
cout<<"The number of 'u'is "<< vow_u<<endl;
system("pause");
return 0;
}
void count(string sentence,int &a,int& e,int& i,int& o,int& u)
{int n=0;
do{
switch(sentence.at(n)){
case 'a':
a++;
break;
case 'e':
e++;
break;
case 'i':
i++;
break;
case 'o':
o++;
break;
case 'u':
u++;
break;
}
n++;
}while(n<sentence.length());
}
how would I put comments in this code???
Explanation / Answer
// comments are added to your program..
#include <iostream> // iostream for input and output
#include <string> // library for string operations.
using namespace std; // for cout and cin ..
void count(string,int&,int&,int&,int&,int&); // count function prototype.
int main() // start of main.
{
string sentence; // variable initialization.
int i=0;
int vow_a=0, vow_e=0,vow_i=0, vow_o=0, vow_u=0;
cout<<"Please enter a sentence. ";
getline(cin,sentence); // input sentence.
count(sentence,vow_a,vow_e,vow_i, vow_o, vow_u); // call function to count chars.
cout<<"The number of 'a' is "<< vow_a<<endl; // print no of a's
cout<<"The number of 'e' is "<< vow_e<<endl;// print no of e's
cout<<"The number of 'i'is "<< vow_i<<endl;// print no of i's
cout<<"The number of 'o'is "<< vow_o<<endl;// print no of o's
cout<<"The number of 'u'is "<< vow_u<<endl;// print no of u's
system("pause"); // wait for key press.
return 0;
}
void count(string sentence,int &a,int& e,int& i,int& o,int& u)
{int n=0;
do{
switch(sentence.at(n)){ // catch each character and observe.
case 'a':
a++; // if it is a increment a
break;
case 'e':
e++; // if it is e increment e
break;
case 'i':
i++; // if it is i increment i
break;
case 'o':
o++; // if it is o increment o
break;
case 'u':
u++; // if it is u increment u
break;
}
n++;
}while(n<sentence.length()); // till end of sentence.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.