# Count Number of Vowels and Consonants in the String

Count Number of Vowels and Consonants in the String ” is a basic problem of string data structure. Here, we are given a string of length ‘n’ and our task is to count the number of vowels and consonants present in the string.

Example:

```Input:
[email protected]#bro
Output:
The number of vowels in [email protected]#bro is 4
The number of consonants in [email protected]#bro is 10```

The steps to program to Count Number of Vowels and Consonants in the String is as follows:

1. Scan Input string.
2. Initialize vowelCount = 0 and consonantCount = 0.
3. Traverse the string in the loop and check whether given character if consonant or vowel.
4. If vowel, increase vowelCount by 1. Else if character is consonant, increase consonantCount by 1.

#### C++ Program to Count Number of Vowels and Consonants in the String is as follows:

```/* Program to Count Number of Vowels and Consonants in the String */
#include<bits/stdc++.h>
#define len 1000
using namespace std;
int main()
{
char str[len];
cout<<"Enter the string:\n";
cin>>str;
int vowelCount = 0;
int consonantCount = 0;
for(int i = 0; i < len && str[i] != ' /* Program to Count Number of Vowels and Consonants in the String */ #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter the string:\n"; cin>>str; int vowelCount = 0; int consonantCount = 0; for(int i = 0; i < len && str[i] != '\0'; i++) { /* If character is vowel, increase vowel count*/ if(str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u') vowelCount++; /* Else if character is alphabet, increase consonantCount */ else if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) consonantCount++; } cout<<"\nThe number of vowels in "<<str<<" is "<<vowelCount; cout<<"\nThe number of consonants in "<<str<<" is "<<consonantCount; } '; i++)
{
/* If character is vowel, increase vowel count*/
if(str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u')
vowelCount++;
/* Else if character is alphabet, increase consonantCount */
else if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
consonantCount++;
}
cout<<"\nThe number of vowels in "<<str<<" is "<<vowelCount;
cout<<"\nThe number of consonants in "<<str<<" is "<<consonantCount;
}

```
```OUTPUT:
Enter the string: [email protected]#bro
The number of vowels in [email protected]#bro is 4
The number of consonants in [email protected]#bro is 10
Time Complexity:
O(n), where n is the length of the string.```

Related Posts: