In C++ write a function that accepts a pointer to a C-string as its argument. Th
ID: 3600064 • Letter: I
Question
In C++
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 consona nts appearing in the string and return that number.
Have the functions perform the following:
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 the number of vowels and consonants in the string
D. enter another string
E exit program
3. The program performs the operation selected by the user and repeats until the user selects E to exit the program
Explanation / Answer
#include<stdio.h>
#include<string.h>
int countOfVowels(char *string)
{
int count=0;
while(*string!='')
{
if(*string=='a' || *string=='A' || *string=='e' || *string=='E' || *string=='i' || *string=='I' || *string=='o' || *string=='O' || *string=='u' || *string=='U')
count++;
string++;
}
return count;
}
int countOfConsonants(char *string)
{
int count=0;
while(*string!='')
{
if(*string!='a' && *string!='A' && *string!='e' && *string!='E' && *string!='i' && *string!='I' && *string!='o' && *string!='O' && *string!='u' && *string!='U')
count++;
string++;
}
return count;
}
int main()
{
char string[100];
printf("Enter a string : ");
gets(string);
while(1)
{
printf("A. Count the number of Vowels in the string ");
printf("B. Count the number of Consonants in the string ");
printf("C. Count the number of Vowels & Consonants in the string ");
printf("D. Enter another string ");
printf("E. Exit Program ");
char choice;
scanf("%c",&choice);
fflush(stdin);
switch (choice)
{
case 'A':
printf("Number of Vowels in the String-%s : %d ",string,countOfVowels(string));
break;
case 'B':
printf("Number of Consonants in the String-%s : %d ",string,countOfConsonants(string));
break;
case 'C':
printf("Number of Vowels and Consonants in the String-%s : %d ",string,strlen(string));
break;
case 'D':
printf("Enter another string : ");
gets(string);
break;
case 'E':
exit(0);
break;
}
}
}