# 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:
Help12me!@study#bro
Output:
The number of vowels in Help12me!@study#bro is 4
The number of consonants in Help12me!@study#bro is 10```

The steps to count the 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 the number of vowels and consonants is as follows:

```#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;
}

```
```OUTPUT:
Enter the string: Help12me!@study#bro
The number of vowels in Help12me!@study#bro is 4
The number of consonants in Help12me!@study#bro is 10
Time Complexity:
O(n), where n is the length of the string.```

Related Posts: