Count total consonants and vowels in a given sting in C++

This C++ program is able to count total number of consonants and vowels present in a given string by using loop and conditional statements. This problem is based on the application of character array which is useful in many places. Following is a simple solution to the above task.

How to count total number of consonants and vowels in a given string in C++

Problem Statement

There is a character array (string) of any size and data that the user provides. Count total number of consonants and vowels present in it.

Definition

  • The English letters viz. ‘a’ , ‘e’ , ‘i’ , ‘o’ and ‘u’ (both upper and lower case) are called vowels.
  • All other alphabets except vowels are known as consonants.

Problem approach

  • Declare a string variable of appropriate length with data given.
  • Analyse every letter and then separate vowels and consonants.
  • Sum the required values for these two in different variables.
  • Print the count on the screen.

Program/ Source code

Following C++ program is able to find count the number of consonants and vowels in a given string, is written and successfully compiled in CodeBlocks v16.01.

Count total consonants and vowels in a given sting in C++

/* C++ program to count total number of consonants and vowels in a given string**
 ** enter string value of your choice**
 ***enter different string values for different results***
 string values is accepted for both upper and lower case....
 */
#include <iostream>
#include<string.h>

using namespace std;
int main()
{
    char string[80];
    cout<<"Enter string: ";
    cin.getline(string,80);

    int vowel=0,consonant=0;                // variables for count of vowel and consonants

    for(int i=0;i<strlen(string);i++){
        if(isalpha(string[i])){                 // condition for alphabet
            if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string[i]=='o'||string[i]=='u'||     // condition for vowel
               string[i]=='A'||string[i]=='E'||string[i]=='I'||string[i]=='O'||string[i]=='U')
                vowel++;
            else
                consonant++;
        }
    }

    cout<<"Count of vowels: "<<vowel<<endl;
    cout<<"Count of consonant: "<<consonant<<endl;

    return 0;
}

Output Example

Enter string: codespeedy
Count of vowels: 4
Count of consonant: 6

Process returned 0 (0x0) execution time : 6.910 s
Press any key to continue.
Enter string: Hello WORLD123
Count of vowels: 3
Count of consonant: 7

Process returned 0 (0x0) execution time : 14.190 s
Press any key to continue.

Program Explanation

  • Declare string of length 80.
  • Take string data by the user.
  • Declare separate variables for vowels and consonants counting.
  • Analyse each letter of string by using for loop.
  • Use isalpha() declared in string.h header to detect alphabet from string.
  • Use conditional operate to separate vowels from other letters.
  • Other alphabets are consonants as per definition.
  • Print the count on the screen.

Also read-

How to remove or eliminate the extra spaces from a given string in C++

Leave a Reply

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