-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvowels-and-consonants.cpp
More file actions
94 lines (81 loc) · 1.87 KB
/
vowels-and-consonants.cpp
File metadata and controls
94 lines (81 loc) · 1.87 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Vowels and Consonants
// Hirchuk Vladyslav
#include <iostream>
#include <cctype>
using namespace std;
bool isVowel(char c)
{
c = toupper(c);
return (c == 'A' || c == 'E' || c == 'I'
|| c == 'O' || c == 'U');
}
int countVowels(char* str)
{
int counter = 0;
while (*str) {
char current = tolower(*str);
if (current == 'a' ||
current == 'e' ||
current == 'i' ||
current == 'o' ||
current == 'u') {
counter++;
}
str++;
}
return counter;
}
int countConsonants(char* str)
{
int counter = 0;
while (*str) {
char current = tolower(*str);
if (isalpha(current) && !isVowel(current)) counter++;
str++;
}
return counter;
}
int countSpaces(char* str)
{
int count = 0;
for (int i = 0; i < strlen(str); i++)
if (isspace(str[i]))
count++;
return count;
}
int main()
{
const int SIZE = 200;
char sentence[SIZE];
cout << "Enter a sentence: ";
cin.getline(sentence, SIZE);
char choice = 'A';
while (toupper(choice) != 'E')
{
cout << "\nYour sentence: " << sentence << endl << endl;
cout << "A) Count the number of vowels in the string" << endl;
cout << "B) Count the number of consonants in the string" << endl;
cout << "C) Count both the vowels and consonants in the string" << endl;
cout << "D) Enter another string" << endl;
cout << "E) Exit the program" << endl;
cin >> choice;
switch (toupper(choice))
{
case 'A': cout << "There are " << countVowels(sentence) << " vowels." << endl;
break;
case 'B': cout << "There are " << countConsonants(sentence) << " consonants." << endl;
break;
case 'C':
cout << "There are " << countVowels(sentence) + countConsonants(sentence) << " vowels and consonants."; // COUNT vowels and consonants DONE
break;
case 'D':
cin.ignore();
cout << "Enter a sentence: ";
cin.getline(sentence, SIZE);
break;
case 'E': break;
default: // Optional error message
break;
}
}
}