// Saliha Babar CS1A Chapter 10, #6 , Page 589
//
/*****************************************************************************
* COUNT VOWELS AND CONSONANTS
* ___________________________________________________________________________
* This program accepts a string up to n characters, and calculates the number
* of vowels and consonants in that string.
* ___________________________________________________________________________
* INPUT
* SIZE : size of the string
* array : array of characters
* choice : user choice from the menu
*
* OUTPUT
* vowelsCount : number of vowels
* consCount : number of consonants
* ***************************************************************************/
#include <iostream>
#include <cctype>
using namespace std;
char* GetString (int SIZE);
int CountVowel (char array[] , int SIZE);
int CountConsonant (char array[] , int SIZE);
int main() {
const int SIZE = 31; // INPUT - size of the string
char *array; // Pointer of the array
int choice; // INPUT - User choice for menu selection
int vowelsCount; // OUTPUT - number of vowels
int consCount; // OUTPUT - number of consonants
char input; // User choice to repeat the menu/exit program
do {
//Get the string from user
array = GetString (SIZE);
// Show the menu to the user
cout << "---------Menu----------\n";
cout << "1- count vowels\n";
cout << "2- count consonants\n";
cout << "3- count consonants and vowels\n";
cout << "Enter your choice\n";
cin >> choice;
cin.ignore();
switch (choice) {
case 1 :
vowelsCount = CountVowel(array, SIZE);
cout << "Number of vowel(s) :" << vowelsCount << endl;
break;
case 2 :
consCount = CountConsonant (array, SIZE);
cout << "Number of consonants(s) :" << consCount << endl;
break;
case 3 :
vowelsCount = CountVowel(array, SIZE);
consCount = CountConsonant (array, SIZE);
cout << "Number of vowel(s) :" << vowelsCount << endl;
cout << "Number of consonants(s) :" << consCount << endl;
}
cout << "\nPress any key to continue with different string\n";
cout << "Enter Y to quit the program\n";
cin >> input;
delete [] array;
}
while (tolower(input) != 'y' && toupper(input) != 'Y');
return 0;
}
char* GetString (int SIZE)
{
char *character;
character = new char [SIZE];
cout << "Enter a string up to " << SIZE -1 << " characters\n";
cin.ignore();
cin.getline (character,SIZE);
// Convert those characters to lowercase
for (int i = 0; i < SIZE ; i++)
{
character[i] = tolower (character[i]);
}
return character;
}
int CountVowel (char array[] , int SIZE)
{
int count = 0;
for (int i = 0; i < SIZE ; i++)
{
if (array[i] == 'a' || array[i] == 'e' || array[i] == 'i' ||
array[i] == 'o' || array[i] == 'u')
count++;
}
return count;
}
int CountConsonant (char array[] , int SIZE)
{
int count = 0;
for (int i = 0; i < SIZE ; i++)
{
if (isalpha(array[i]) && array[i] != 'a' && array[i] != 'e' && array[i] != 'i' &&
array[i] != 'o' && array[i] != 'u')
count++;
}
return count;
}