The program to count the frequency of vowels, consonants and white spaces for a given word or a sentence is a string manipulation program. A loop with conditions check the given input string and count the number of vowels, consonants, and white spaces.

Problem Definition

In programming languages, every string is like an array with each character of the string hold a position in this array. The last character is null character by default in C/C++.

Programming languages like Java, Python, and JavaScript do not add a null at the end of a string, but they treat a string as sequence of individual characters as an array.

Using this array logic, the program read a word or a sentence during execution and check each and every character of input string. It counts the number of vowels, consonants and white spaces.

For example, the array representation of word – SPIDERMAN as follows.

\begin{aligned} &0 \hspace{5px}1  \hspace{5px}2\hspace{5px} 3 \hspace{5px}4 \hspace{5px}5 \hspace{5px}6\hspace{5px} 7\hspace{5px} 8 \hspace{5px}9 \\ 
&S \hspace{3px} P\hspace{3px} I \hspace{3px}D \hspace{3px}E \hspace{3px}R \hspace{3px}M\hspace{3px} A \hspace{3px}N \hspace{3px}\backslash\empty\end{aligned}

Note:- The Null character is added to all the strings in C by default.

Flowchart – Program to Count Frequency of Vowels

Figure 1 - Flowchart for Program to count the frequency of Vowels.
Figure 1 – Flowchart for Program to count the frequency of Vowels.

Program Codes – Program to Count Frequency of Vowels

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    int a=0, e=0, i=0, o=0, u=0;
    int consonant=0, whitespace=0;

    cout << "Enter a sentence: ";
    getline(cin, str);

    for (char ch : str)
    {
        if (ch=='a'||ch=='A') a++;
        else if (ch=='e'||ch=='E') e++;
        else if (ch=='i'||ch=='I') i++;
        else if (ch=='o'||ch=='O') o++;
        else if (ch=='u'||ch=='U') u++;
        else if (isalpha(ch)) consonant++;
        else if (ch == ' ') whitespace++;
    }

    cout << "a : " << a << endl;
    cout << "e : " << e << endl;
    cout << "i : " << i << endl;
    cout << "o : " << o << endl;
    cout << "u : " << u << endl;
    cout << "Consonants : " << consonant << endl;
    cout << "Whitespaces : " << whitespace << endl;

    return 0;
}
import java.util.Scanner;

public class VowelCount {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a=0, e=0, i=0, o=0, u=0;
        int consonant=0, whitespace=0;

        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();

        for (char ch : str.toCharArray()) {
            if (ch=='a'||ch=='A') a++;
            else if (ch=='e'||ch=='E') e++;
            else if (ch=='i'||ch=='I') i++;
            else if (ch=='o'||ch=='O') o++;
            else if (ch=='u'||ch=='U') u++;
            else if (Character.isLetter(ch)) consonant++;
            else if (ch == ' ') whitespace++;
        }

        System.out.println("a : " + a);
        System.out.println("e : " + e);
        System.out.println("i : " + i);
        System.out.println("o : " + o);
        System.out.println("u : " + u);
        System.out.println("Consonants : " + consonant);
        System.out.println("Whitespaces : " + whitespace);

        sc.close();
    }
}
s = input("Enter a sentence: ")

a = e = i = o = u = consonant = whitespace = 0

for ch in s:
    if ch in 'aA': a += 1
    elif ch in 'eE': e += 1
    elif ch in 'iI': i += 1
    elif ch in 'oO': o += 1
    elif ch in 'uU': u += 1
    elif ch.isalpha(): consonant += 1
    elif ch == ' ': whitespace += 1

print("a :", a)
print("e :", e)
print("i :", i)
print("o :", o)
print("u :", u)
print("Consonants :", consonant)
print("Whitespaces :", whitespace)

Apart from being a string program, we used built-in functions from C standard library to get string input (fgets) and compute the length of the given input string (strlen).

The C++ code uses getline() command to read the sentence.

Similarly, the scanner object sc is used to read the input text in Java program. It uses nextline() method which can read an entire line.

The input() function in Python is capable of reading a line of text from user.

After input, the operations on string to extract vowels, consonants, and spaces is similar in all four programming languages for the program.

Output

The output of the program is given below. When the sentence ” A large reptile” is entered, the program reads all the vowels, consonants and white spaces and print the results.

ENTER A WORD OR A SENTENCE:
A large reptile
_____________________________________________
a :1
e :3
i :1
o :0
u :0

CONSONANTS :8
WHITESPACES :2