Programming Challenges Write another function that accepts a C-string as its arg
ID: 3589195 • Letter: P
Question
Programming Challenges Write another function that accepts a C-string as its argument. This function should ount the number of consonants appearing in the string and return that number. Demonstrate the two functions in a program that performs the following steps: The user is asked to enter a string. 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 1. 2. The program performs the operation selected by the user and repeats until th user selects E, to exit the program. 3. 1ast names. The nam
Explanation / Answer
#include <iostream>
using namespace std;
void displayMenu(){
cout<<"-----------Menu----------------- ";
cout << "A. Count No. of Vowels ";
cout << "B. Count No. of Consonants ";
cout << "C. Count both Vowels and Consonants ";
cout << "D. Enter another String ";
cout << "E. Exit the program ";
}
//counting vowels
int count_vowels(char* str){
int vowels=0;
for(int i = 0; str[i]!=''; ++i)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
++vowels;
}
}
return vowels;
}
//counting consonants
int count_consonants(char* str){
int consonants=0;
for(int i = 0; str[i]!=''; ++i)
{
if(!(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')&&((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')))
{
++consonants;
}
}
return consonants;
}
int main()
{
//array to take input
char str[255];
//variable for to get response
char input;
cout << "Enter a string: ";
//get input
cin.getline(str, 255);
do{
//display menu
displayMenu();
//get response
cin>>input;
cin.ignore();
//Perform action accordingly
switch(input){
case 'A':
case 'a':
cout<<"No. of Vowels "<<count_vowels(str)<<endl;
break;
case 'B':
case 'b':
cout<<"No. of Consonats "<<count_consonants(str)<<endl;
break;
case 'C':
case 'c':
cout<<"No. of Vowels"<<count_vowels(str)<<endl;
cout<<"No. of Consonats "<<count_consonants(str)<<endl;
break;
case 'D':
case 'd':
cout<<"Enter another string ";
cin.getline(str, 255);
break;
case 'E':
case 'e':
break;
default:
cout<<"Invalid Selection"<<endl;
}//end of switch
}while(!((input == 'E')||(input == 'e')));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.