C++ Program to find the number of vowels and consonants in a string
In this program, You will learn how to find number of vowels and consonants in a string in C++.
Learn Java
Total vowels is:4
Total consonants are:5
Example: How to find a number of vowels and consonants in a string in C++.
#include<iostream>
using namespace std;
int main() {
char str[100];
int i = 0, vols = 0, cons = 0;
cout << "Enter string value:";
cin.getline(str, 100);
while (str[i] != '\0') {
if (str[i] == 'a' || str[i] == 'e' ||
str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u' || str[i] == 'A' ||
str[i] == 'E' || str[i] == 'I' ||
str[i] == 'O' || str[i] == 'U') {
vols++;
} else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
cons++;
}
i++;
}
cout << "Total Vowels is:" << vols;
cout << "\nTotal Consonants is:" << cons;
return 0;
}
Output:
Enter string value:Xiith
Total Vowels is:2
Total Consonants is:3