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:

  1. Program to Check if String is alphabetic or numeric or alpha-numeric.
  2. Program to copy one String to Another.
  3. Program to Concatenate Two Strings.
  4. Program to Check whether given String is Palindrome or not.
  5. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  6. Program to count total number of words in a string.
  7. Program to find smallest and largest word of the String.
  8. Program to find Most Frequent Character of the String.
  9. Program to Remove all the blank spaces from the String.
  10. Program to check if String is isogram or not.
  11. Program to Reverse Each word of the String.
  12. Program to Print All the Substring of the Given String.
  13. Program to find longest palindromic Substring.
  14. Program to check Anagram Strings.
  15. Program to check whether two Strings are Rotation of each other or not.
  16. Program to check Palindromic Anagram.
  17. Program to print all the Palindromic Substring of the String.
  18. Program to check Panagram String.
  19. Program to find first non-repeating character of the String.
  20. Program to merge two sorted arrays.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *