-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathEx5_10.cpp
More file actions
36 lines (30 loc) · 1.06 KB
/
Ex5_10.cpp
File metadata and controls
36 lines (30 loc) · 1.06 KB
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
// Classifying the letters in a C-style string
import <iostream>;
#include <cctype>
int main()
{
const int max_length {100}; // Array size
char text[max_length] {}; // Array to hold input string
std::cout << "Enter a line of text:" << std::endl;
// Read a line of characters including spaces
std::cin.getline(text, max_length);
std::cout << "You entered:\n" << text << std::endl;
size_t vowels {}; // Count of vowels
size_t consonants {}; // Count of consonants
for (int i {}; text[i] != '\0'; i++)
{
if (std::isalpha(text[i])) // If it is a letter...
{
switch (std::tolower(text[i]))
{ // ...check lowercase...
case 'a': case 'e': case 'i': case 'o': case 'u':
++vowels; // ...it is a vowel
break;
default:
++consonants; // ...it is a consonant
}
}
}
std::cout << "Your input contained " << vowels << " vowels and "
<< consonants << " consonants." << std::endl;
}