6. Vowels and Consonants Write a function that accepts a pointer to a C-string a
ID: 3669921 • Letter: 6
Question
6. Vowels and Consonants
Write a function that accepts a pointer to a C-string as its argument. The function
should count the number of vowels appearing in the string and return that number.
Write another function that accepts a pointer to a C-string as its argument. This function
should count the number of consonants appearing in the string and return that
number.
Demonstrate these two functions in a program that performs the following steps:
1. The user is asked to enter a string.
2. The program displays the following 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
3. The program performs the operation selected by the user and repeats until the user
selects E to exit the program.
c++
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
bool isNotVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
return false;
}
return true;
}
int countVowel(char x[]){
int n,i,s=0;
n=strlen(x);
for(i=0;i<n;++i)
{
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
{
s=s+1;
}
}
return s;
}
int countConsonant(char x[]){
int n,i,s=0;
n=strlen(x);
for(i=0;i<n;++i)
{
if(((x[i]>='a'&& x[i]<='z')|| (x[i]>='A'&& x[i]<='Z')) && isNotVowel(x[i]))
s++;
}
return s;
}
int main(){
char str[100];
char c ='Y';
while(c!='E'){
cout<<"Enter a string: ";
cin.ignore();
cin.getline(str, 100);
int vowel = countVowel(str);
int cons = countConsonant(str);
cout<<"Number of vowel: "<<vowel<<endl;
cout<<"Number of Consonent: "<<cons<<endl;
cout<<"Vowel + consonent: "<<(vowel+cons)<<endl;
cout<<"To exit type E, else Y to continue... ";
cin>>c;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.