forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.Ex2 NumVowels.cpp
40 lines (29 loc) · 927 Bytes
/
16.Ex2 NumVowels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <iostream>
using namespace std;
// Find the number of character 'chToFind' in string "strInput"
int GetNumCharacters(string& strInput, char chToFind)
{
int nNumCharactersFound = 0;
size_t nCharOffset = strInput.find(chToFind);
while (nCharOffset != string::npos)
{
++nNumCharactersFound;
nCharOffset = strInput.find(chToFind, nCharOffset + 1);
}
return nNumCharactersFound;
}
int main()
{
cout << "Please enter a string:" << endl << "> ";
string strInput;
getline(cin, strInput);
int nNumVowels = GetNumCharacters(strInput, 'a');
nNumVowels += GetNumCharacters(strInput, 'e');
nNumVowels += GetNumCharacters(strInput, 'i');
nNumVowels += GetNumCharacters(strInput, 'o');
nNumVowels += GetNumCharacters(strInput, 'u');
// DIY: handle capitals too..
cout << "The number of vowels in that sentence is:" << nNumVowels;
return 0;
}