/* Vowels And Consonants - This program contains a function that accepts
a pointer to a C-string as its argument. The function counts the number
of vowels appearing in the string and returns that number.
The second function accepts a pointer to a C-string as its argument.
It counts the number of consonants appearing in the string and returns
that number.
The functions are demonstrated by performing the following steps:
* The user is asked to enter a string.
* The program displays the following menu:
* Count the number of vowels in the string
* Count the number of consonants in the string
* Count both the vowels and consonants in the string
* Enter another string
* Exit the program
* The program performs the operation selected by the user and
repeats until the user selects E to exit the program. */
#include "Utility.h"
/* Provides a basic menu */
int menu();
/* Allows the user to make a menu choice,
returns the menu choice */
int menuOptions();
/* Determines whether a character is a vowel,
returns the result */
bool vowel(char);
/* Counts the vowels in a C-string,
returns the number of vowels found */
int countVowels(char *);
/* Counts the consonants in a C-string,
returns the number of consonants found */
int countConsonants(char *);
int main()
{
menu();
pauseSystem();
return 0;
}
/* **********************************************************
Definition: menu
This function provides a basic menu.
********************************************************** */
int menu()
{
const int NUM_WORDS = 200;
int numVowels = 0,
numConsonants = 0,
menuChoice = 0;
char sentence[NUM_WORDS];
cout << "\n\tVOWEL AND CONSONANT COUNT\n\n"
<< "\tEnter a sentence, " << (NUM_WORDS - 1)
<< " characters maximum in length:\n\t";
cin.getline(sentence, NUM_WORDS);
do
{
menuChoice = menuOptions();
cin.ignore();
switch (menuChoice)
{
case 1:
{
cout << "\n\tYour sentence contains: "
<< (numVowels = countVowels(sentence))
<< " vowels.\n\n";
}
break;
case 2:
{
cout << "\n\tYour sentence contains: "
<< (numConsonants = countConsonants(sentence))
<< " consonants.\n\n";
}
break;
case 3:
{
cout << "\n\tYour sentence contains: "
<< (numVowels = countVowels(sentence))
<< " vowels and "
<< (numConsonants = countConsonants(sentence))
<< " consonants.\n\n";
}
break;
case 4:
{
cout << "\n\tEnter another sentence:\n\t";
cin.getline(sentence, NUM_WORDS);
}
break;
case 5:
{
cout << "\n\tHave a nice day!\n\n";
}
}
} while (menuChoice != 5);
return 0;
}
/* **********************************************************
Definition: displayOptions
This function displays the main menu options.
********************************************************** */
int menuOptions()
{
const int VOWEL_COUNT = 1,
CONSONANT_COUNT = 2,
VOWEL_CONSONANT_COUNT = 3,
AGAIN = 4,
EXIT = 5;
int menuChoice = 0;
cout << "\n\tMain Menu\n"
<< "\t----------\n\n"
<< "\t1. Count the number of vowels in the string\n"
<< "\t2. Count the number of consonants in the string\n"
<< "\t3. Count both the vowels and consonants in the string\n"
<< "\t4. Enter another string\n"
<< "\t5. Exit\n\n"
<< "\tEnter your choice: ";
cin >> menuChoice;
/* Input validation */
while (menuChoice < VOWEL_COUNT || menuChoice > EXIT)
{
cout << "\tEnter your choice: ";
cin >> menuChoice;
}
return menuChoice;
}
/* **********************************************************
Definition: isVowel
This function determines if a character is a vowel. The
result is returned.
********************************************************** */
bool vowel(char vowel)
{
if (vowel == 'a' || vowel == 'i' || vowel == 'e' || vowel == 'o' ||
vowel == 'u' || vowel == 'A' || vowel == 'I' || vowel == 'I' ||
vowel == 'O' || vowel == 'U')
{
return true;
}
else
{
return false;
}
}
/* **********************************************************
Definition: countVowels
This function accepts a C-string object as argument. It
counts the number of vowels and returns this number.
********************************************************** */
int countVowels(char *wordPtr)
{
unsigned int index = 0,
vowelCount = 0;
for (index = 0; index < strlen(wordPtr); index++)
{
if (vowel(wordPtr[index])==true)
{
++vowelCount;
}
}
return vowelCount;
}
/* **********************************************************
Definition: countConsonants
This function accepts a C-string object as argument. It
counts the number of consonants in a sentence and returns
this number.
********************************************************** */
int countConsonants(char *wordPtr)
{
unsigned int index = 0,
consonantCount = 0;
bool isConsonant = false;
for (index = 0; index < strlen(wordPtr); index++)
{
if (vowel(wordPtr[index]) == true)
{
isConsonant = false;
}
else if (isalpha(wordPtr[index]))
{
isConsonant = true;
consonantCount++;
}
}
return consonantCount;
}
Wednesday, April 19, 2017
Programming Challenge 10.6 - Vowels And Consonants
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment