IsVowel Function


Program Listing

IsVowelFunc.cpp

/************************************************************************
*                                                                       *
*   Program:    IsVowelFunc                                             *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

//  Function prototype

bool IsVowel(char c);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
************************************************************************/

int main() {

    int VowelCount = 0;
    cout << "Enter a string of text, terminated by CTRL-D:  " << endl << flush;
    char c;

//  Read the characters, one by one, counting the vowels

    while (cin.get(c)) {
        if (IsVowel(c))
            VowelCount++;
    }

//  Print the results

    cout << endl << endl;
    cout << "There were " << VowelCount << " vowels" << endl;
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   IsVowel                                                 *
*                                                                       *
************************************************************************/

bool IsVowel(char c) {
    c = toupper(c);

//  Switch on the character

    switch (c) {
    
    //  Character is a vowel
    
        case 'A': case 'E': case 'I': 
        case 'O': case 'U': case 'Y':
            return true;
    
    //  Character is not a vowel
    
        default:
            return false;
    }
}

Sample Run

Enter a string of text, terminated by CTRL-D:  
Once upon a time, they lived happily ever after.

There were 18 vowels



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:36 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/IsVowelFunc.html

Return to Coms 261 home page