I am coming to a problem where I am writing 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. Then, I am writing 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.
Am I violating any of the following restrictions?
- No global variables
- No labels or go-to statements
- No infinite loops, examples include: for(;;), while(1), while(true), do{//code}while(1);
- No break statements to exit loops
Here is my code:
#include<iostream>
using namespace std;
//Function prototypes
int Vowel_count( char *str);
int Consonant_count(char *str);
int main()
{
char String[30];
char choice;
int Nvowels,Nconsonant;
//Input a string
cout<<"Enter a string:";
cin.getline(String ,30);
do
{
//Displaying Menu
cout<<"\nMENU"<<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;
//Inputting choice
cout<<"Enter choice";
cin>>choice;
switch(choice)
{
case 'A':
//Function call to get number of Vowels
Nvowels=Vowel_count(String);
cout<<"Number of vowels is:"<<Nvowels<<endl;
break;
case 'B':
//Function call to get number of consonants
Nconsonant=Consonant_count(String);
//Outputting number of Consonants
cout<<"Number of Consonants is:"<<Nconsonant<<endl;
break;
case 'C':
//Function call to get number of Vowels
Nvowels=Vowel_count(String);
//Function call to get number of consonants
Nconsonant=Consonant_count(String);
//Outputting Both Vowels and Consonants
cout<<"Number of vowels is:"<<Nvowels<<endl;
cout<<"Number of Consonants is:"<<Nconsonant<<endl;
break;
case 'D': //Inputting another string
cout<<"Enter another String:";
fflush(stdin);
cin.getline(String ,30);
break;
case 'E': exit(0);
break;
}//End of Switch
}while(choice !='E');//End Do while
//Pause system for a while
system("pause");
}//End main
//Function Definitions
int Vowel_count( char *str)
{
int count=0;//Local variable
//Checks for all characters in C-string
while(*str!='\0')
{
if(*str=='a'||*str=='e'||*str=='i'||*str=='o'||*str=='u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}//End of Vowel_count
int Consonant_count(char *str)
{
int count=0;
//Checks for all characters in C-string
while(*str!='\0')
{ //Checks for consonants
if(*str!='a'&&*str!='e'&&*str!='i'&&*str!='o'&&*str!='u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}