I need the user to be asked to enter a string. The program should display this menu: A count the number of vowels in the string B Count the number of consonants in the string C count both the vowels and consonants in the string D enter another string E exit the program. The program should repeat until the user selects E I already asked this quesiton but I would like the menu not to show up mutlibe times. I would like the menu to show up A-E once not with 10 diffrent outputs.
I need help writing a C++ code. The code needs to count the number of consonants appearing in the string and return that number.
I need the user to be asked to enter a string. The program should display this menu:
A count the number of vowels in the string
B Count the number of consonants in the string
C count both the vowels and consonants in the string
D enter another string
E exit the program.
The program should repeat until the user selects E
I already asked this quesiton but I would like the menu not to show up mutlibe times. I would like the menu to show up A-E once not with 10 diffrent outputs.
#include<iostream>
using namespace std;
//method to check a char is vowel or not
bool checkVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return true;//returning true if it is vowel
return false;//returning false if it is not vowel
}
//method to check a char is consonant or not
bool checkConsonant(char c)
{
if(!checkVowel(c) &&( (65<=c&&c<=91) || (97<=c&&c<=122)))//if it is not vowel and it is alphabet then it is consonent
return true;
return false;
}
//method to display menu
void menu(){
cout<<"A count the number of vowels in the string\n";
cout<<"B Count the number of consonants in the string\n";
cout<<"C count both the vowels and consonants in the string\n";
cout<<"D enter another string\n";
cout<<"E exit the program.\n";
}
int main(){
//declaring string variable
string str;
//reading string from user
cout<<"Enter a string:";
cin>>str;
while(true){
//now displaying menu
menu();
//now reading option from menu
char c ;
cout<<"Enter option:";
cin>>c;
if(c=='A')
{
//counting vowels
int count=0;
for(int i=0;str[i]!='\0';i++)
if(checkVowel(str[i]))
count++;//if vowel then increasing count
//now displaying vowel count
cout<<"Count of vowels:"<<count<<endl;
}
else if(c=='B')
{
//counting consonants
int count=0;
for(int i=0;str[i]!='\0';i++)
if(checkConsonant(str[i]))
count++;//if consonant then increasing count
//now displaying consonant count
cout<<"Count of consonants:"<<count<<endl;
}
else if(c=='C')
{
int countV=0,countB=0;
for(int i=0;str[i]!='\0';i++){
if(checkVowel(str[i]))
countV++;//if vowel then increasing count
if(checkConsonant(str[i]))
countB++;//if consonant then increasing count
}
//now displaying vowel count
cout<<"Count of vowels:"<<countV<<endl;
//now displaying consonant count
cout<<"Count of consonants:"<<countB<<endl;
}
else if(c=='D')
{
//reading another string
cout<<"Enter another string:";
cin>>str;
}
else if(c=='E')
break;//stopping if E is entered
}
return 0;
}
#include<iostream>
using namespace std;
//method to check a char is vowel or not
bool checkVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return true;//returning true if it is vowel
return false;//returning false if it is not vowel
}
//method to check a char is consonant or not
bool checkConsonant(char c)
{
if(!checkVowel(c) &&( (65<=c&&c<=91) || (97<=c&&c<=122)))//if it is not vowel and it is alphabet then it is consonent
return true;
return false;
}
//method to display menu
void menu(){
cout<<"A count the number of vowels in the string\n";
cout<<"B Count the number of consonants in the string\n";
cout<<"C count both the vowels and consonants in the string\n";
cout<<"D enter another string\n";
cout<<"E exit the program.\n";
}
int main(){
//declaring string variable
string str;
//reading string from user
cout<<"Enter a string:";
getline(cin,str); // to read white spaces as well
//now displaying menu
menu();
char c = '#';
while(c != 'E'){ // menu will not be displayed and program will exit on entering E
//now reading option from menu
cout<<"Enter option:";
cin>>c;
if(c=='A')
{
//counting vowels
int count=0;
for(int i=0;str[i]!='\0';i++)
if(checkVowel(str[i]))
count++;//if vowel then increasing count
//now displaying vowel count
cout<<"Count of vowels:"<<count<<endl;
}
else if(c=='B')
{
//counting consonants
int count=0;
for(int i=0;str[i]!='\0';i++)
if(checkConsonant(str[i]))
count++;//if consonant then increasing count
//now displaying consonant count
cout<<"Count of consonants:"<<count<<endl;
}
else if(c=='C')
{
int countV=0,countB=0;
for(int i=0;str[i]!='\0';i++){
if(checkVowel(str[i]))
countV++;//if vowel then increasing count
if(checkConsonant(str[i]))
countB++;//if consonant then increasing count
}
//now displaying vowel count
cout<<"Count of vowels:"<<countV<<endl;
//now displaying consonant count
cout<<"Count of consonants:"<<countB<<endl;
}
else if(c=='D')
{
//reading another string
cout<<"Enter another string:";
cin>>str;
}
else if(c=='E')
break;//stopping if E is entered
}
return 0;
}
Step by step
Solved in 3 steps with 4 images