Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Vowel and Consonant Frequency

Updated 21 Oct 2026

This program, written in the main method alone, reads a sentence and counts how many times each vowel (A, E, I, O, U) occurs, how many vowels there are in all and how many consonants. A frequency array of five elements holds the five vowel counts, and the whole sentence is scanned once, character by character.

Definition

The vowels are A, E, I, O, U in capital or small form. A consonant here is any letter that is not a vowel (so y is a consonant). Digits, spaces and punctuation marks are not letters and are ignored.

  • Computer Science 2024! has 6 vowels (E = 3, I = 1, O = 1, U = 1) and 9 consonants; the digits and ! are ignored.
  • Rhythm myth 123 has 0 vowels and 10 consonants.
  • A frequency array keeps one counter per item. Here freq[0] counts A, freq[1] counts E, freq[2] counts I, freq[3] counts O and freq[4] counts U.

Two choices made in this program:

  • No library shortcut is used for case. A small letter is turned into its capital by subtracting 32 from its ASCII value ('a' is 97 and 'A' is 65).
  • All five vowels are printed, with 0 for a vowel that does not occur.
  • For an empty line the program prints Empty line: nothing to count.

Algorithm

  1. Read the sentence into str. Create the array freq of size 5 (the counts of A, E, I, O, U) and set vowels = 0 and consonants = 0.
  2. For every character c of the sentence: if it is a small letter, subtract 32 from its ASCII value to get the capital, so that the comparison ignores the case.
  3. If c is now a letter (between 'A' and 'Z'), find p, its index in A, E, I, O, U, or -1 if it is not a vowel.
    • If p >= 0, add 1 to freq[p] and to vowels.
    • Otherwise it is a consonant: add 1 to consonants.
  4. Digits, spaces and punctuation marks are not letters, so they are ignored.
  5. If the line was empty, print a message. Otherwise print the count of each vowel, vowels and consonants.

Program

One for loop in main scans the sentence; the chain of if statements turns a vowel into its index p in the array freq.

import java.util.Scanner;

/* Program VowelCount reads a sentence and displays how many times each vowel occurs,
   the total vowels and the total consonants. Digits, spaces and punctuation
   are ignored. */
public class VowelCount
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();              // the sentence entered by the user
        int freq[] = new int[5];                 // freq[0..4] = counts of A, E, I, O, U
        int vowels = 0;                          // total number of vowels
        int consonants = 0;                      // total number of consonants
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (c >= 'a' && c <= 'z')            // bring a small letter to a capital (ASCII value reduced by 32)
                c = (char) (c - 32);             // for a case-insensitive comparison
            if (c >= 'A' && c <= 'Z')            // only letters are counted
            {
                int p = -1;                      // index of the vowel in A, E, I, O, U, or -1 if not a vowel
                if (c == 'A') p = 0;
                else if (c == 'E') p = 1;
                else if (c == 'I') p = 2;
                else if (c == 'O') p = 3;
                else if (c == 'U') p = 4;
                if (p >= 0)                      // a vowel
                {
                    freq[p]++;
                    vowels++;
                }
                else                             // a letter that is not a vowel
                    consonants++;
            }
        }
        if (str.length() == 0)
        {
            System.out.println("Empty line: nothing to count");
        }
        else
        {
            String v = "AEIOU";
            for (int i = 0; i < 5; i++)
                System.out.println(v.charAt(i) + " = " + freq[i]);
            System.out.println("Total vowels = " + vowels);
            System.out.println("Total consonants = " + consonants);
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
strStringThe sentence typed by the user
freqint[]freq[0] to freq[4] count the vowels A, E, I, O, U
vowelsintTotal number of vowels
consonantsintTotal number of consonants
iintLoop counter over the characters, then over the five vowels
ccharThe character being examined, changed to a capital if it is a small letter
pintIndex of the vowel in A, E, I, O, U; -1 if the letter is not a vowel
vStringThe five vowels AEIOU, used to print their names

Sample input and output

Sample 1

Enter a sentence: Computer Science 2024!
A = 0
E = 3
I = 1
O = 1
U = 1
Total vowels = 6
Total consonants = 9

Sample 2

Enter a sentence: Rhythm myth 123
A = 0
E = 0
I = 0
O = 0
U = 0
Total vowels = 0
Total consonants = 10

Sample 3

Enter a sentence: AEIOU aeiou   Uu
A = 2
E = 2
I = 2
O = 2
U = 4
Total vowels = 12
Total consonants = 0

Sample 4

Enter a sentence: Bee Eat!
A = 1
E = 3
I = 0
O = 0
U = 0
Total vowels = 4
Total consonants = 2

One value per line

Dry run

Input Bee Eat!. Before the loop freq is {0, 0, 0, 0, 0} and vowels and consonants are 0. Each row is one pass of the loop.

istr.charAt(i)cpActionfreq (A E I O U)vowelsconsonants
0BB-1consonant0 0 0 0 001
1eE1vowel0 1 0 0 011
2eE1vowel0 2 0 0 021
3spacespacenot a letterignored0 2 0 0 021
4EE1vowel0 3 0 0 031
5aA0vowel1 3 0 0 041
6tT-1consonant1 3 0 0 042
7!!not a letterignored1 3 0 0 042

The program then prints A = 1, E = 3, I = 0, O = 0, U = 0, Total vowels = 4 and Total consonants = 2, the same as the real run above.

Variations

  1. Count vowels using recursion "Write a program to count the number of vowels in a string using recursive technique." See Count Vowels.
  2. Frequency of every letter "Write a program to accept a sentence and print the frequency of each letter of the alphabet that occurs in it."
  3. Replace vowels "Write a program to accept a word and replace every vowel with *."
  4. Vowel-bounded words "Write a program to accept a sentence and print the words that begin and end with a vowel."