#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int vowels = 0, consonants = 0;
cout << "Enter a string: ";
getline(cin, str);
for (int i = 0; i < str.length(); i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if (ch >= 'a' && ch <= 'z') {
consonants++;
}
}
cout << "The number of vowels in the string is: " << vowels << endl;
cout << "The number of consonants in the string is: " << consonants << endl;
return 0;
}
Enter a string: Welcome to freetimelearning.com
The number of vowels in the string is: 12
The number of consonants in the string is: 16
string
, reads the input string using getline(),
and then uses a for
loop to iterate through each character in the string
. vowel (a, e, i, o, u)
or a consonant (any other alphabetical character). lowercase
before checking to ensure that both uppercase
and lowercase vowels
are counted. Finally, it prints out the number of vowels
and consonants in the string
.