Need help ASAP! Computer Science C++ code. The program below counts vowels and c
ID: 3568061 • Letter: N
Question
Need help ASAP! Computer Science C++ code. The program below counts vowels and consonants.
#include
#include
using namespace std;
int Vowel_count( char *str);
int Consonant_count(char *str);
int main()
{
char String[100];
char choice;
int Nvowels,Nconsonant;
cout<<"Enter a string: ";
cin.getline(String, 100);
do
{
cout<<" MENU"< cout<<" (A) Count the number of vowels in the string"< cout<<" (B) Count the number of Consonants in the string"< cout<<" (C) Count both the vowels and consonants in the string"< cout<<" (D) Enter another string"< cout<<" (E) Exit program"< cout<<"Enter choice";
cin>>choice;
switch(choice)
{
case 'A':
case 'a':
Nvowels=Vowel_count(String);
cout<<"Number of vowels is:"< break;
case 'B':
case 'b':
Nconsonant=Consonant_count(String);
cout<<"Number of Consonants is:"< break;
case 'C':
case 'c':
Nvowels=Vowel_count(String);
Nconsonant=Consonant_count(String);
cout<<"Number of vowels is:"< cout<<"Number of Consonants is:"< break;
case 'D':
case 'd':
cout<<"Enter another String:";
cin.getline(String ,100);
break;
case 'E':
case 'e':
exit(0);
break;
}
}while(choice !='E');
}
int Vowel_count( char *str)
{
int count=0;
while(*str!='')
{
if(*str=='a'||*str=='e'||*str=='i'||*str=='o'||*str=='u')
count++;
str++;
}
return count;
}
int Consonant_count(char *str)
{
int count=0;
while(*str!='')
{
if(*str!='a'&&*str!='e'&&*str!='i'&&*str!='o'&&*str!='u')
count++;
str++;
}
return count;
}
Please update/fix the code above so it can
1) ignore whitespace, commas, question marks or any characters except A-Z, a-z.
2) Read uppercase vowels when counting vowels
3) Should not read uppercase vowels when counting consonants
Also, choice "D" does not work, please fix.
Last request, when the user is inputting the string, it should be enclosed in quotation marks.
Please post your program output!
Here's an example program output:
Thank you very much!
Program OutputEnter a string: "Twas brillig, and the slithy toves" A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : a The string has 8 vowels. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : b The string has 20 consonants. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : c The string has 8 vowels and 20 consonants. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : d Enter a string: "Did gyre and gimble in the wabe" A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : A The string has 9 vowels. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : B The string has 16 consonants. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : C The string has 9 vowels and 16 consonants. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : D Enter a string: "All mimsy were the borogoves" A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : c The string has 9 vowels and 15 consonants. A) Count the vowels in the string B) Count the consonants in the string C) Count both vowels and consonants D) Enter another string E) Exit this program Enter A, B, C, D, or E. : e Goodbye!
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<stdio.h>
using namespace std;
int Vowel_count( char *str);
int Consonant_count(char *str);
int main()
{
char String[100];
char choice;
int Nvowels,Nconsonant;
cout<<"Enter a string: ";
cin.getline(String, 100);
do
{
cout<<" MENU"<<endl;
cout<<" (A) Count the number of vowels in the string"<<endl;
cout<<" (B) Count the number of Consonants in the string"<<endl;
cout<<" (C) Count both the vowels and consonants in the string"<<endl;
cout<<" (D) Enter another string"<<endl;
cout<<" (E) Exit program"<<endl;
cout<<"Enter choice: ";
cin>>choice;
switch(choice)
{
case 'A':
case 'a':
Nvowels=Vowel_count(String);
cout<<"Number of vowels is: "<<Nvowels<<endl;
break;
case 'B':
case 'b':
Nconsonant=Consonant_count(String);
cout<<"Number of Consonants is:"<<Nconsonant<<endl;
break;
case 'C':
case 'c':
Nvowels=Vowel_count(String);
Nconsonant=Consonant_count(String);
cout<<"Number of vowels is:"<<Nvowels<<endl;
cout<<"Number of Consonants is:"<<Nconsonant<<endl;
break;
case 'D':
case 'd':
cout<<"Enter another String:";
fflush(stdin);
cin.getline(String ,100);
break;
case 'E':
case 'e':
exit(0);
break;
}
}while(choice !='E');
}
int Vowel_count( char *str)
{
int count=0;
while(*str!='')
{
if(*str=='a'||*str=='e'||*str=='i'||*str=='o'||*str=='u' || *str=='A'||*str=='E'||*str=='I'||*str=='O'||*str=='U')
count++;
str++;
}
return count;
}
int Consonant_count(char *str)
{
int count=0;
while(*str!='')
{
if(*str != 'a' && *str!= 'e' && *str !='i' && *str!='o' && *str != 'u' && *str != 'A' && *str!= 'E' && *str !='I' && *str!='O' && *str != 'U' && ((*str > 'A' && *str <= 'Z') || (*str > 'a' && *str <= 'z')))
count++;
str++;
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.