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

ISC Practicals and Theory - Vowel and Consonant Frequency (OOP)

Updated 21 Oct 2026

This version counts each vowel (A, E, I, O, U), the total vowels and the total consonants of a sentence, with the sentence passed to a parameterised constructor. main reads the line, creates the object with it and calls the methods. The counting is done with a frequency array of five elements.

Definition

The vowels are A, E, I, O, U in capital or small form; a consonant is any other letter. Digits, spaces and punctuation are ignored. The array freq has one counter per vowel: freq[0] for A, freq[1] for E, freq[2] for I, freq[3] for O and freq[4] for U.

  • Computer Science 2024! has 6 vowels (E = 3, I = 1, O = 1, U = 1) and 9 consonants.
  • Bee Eat! has 4 vowels (A = 1, E = 3) and 2 consonants.

Choices made in this program:

  • Capitals and small letters are matched by turning a small letter into a capital with ASCII arithmetic (subtract 32), not with toLowerCase() or Character.isLetter().
  • 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. main reads the whole line into s and creates the object ob with new VowelFrequency(s).
  2. The constructor stores s in str, creates freq with five elements and sets vowels and consonants to 0.
  3. count() visits each position i of str:
    • toUpper() converts a small letter to a capital.
    • If the result lies between 'A' and 'Z' it is a letter, otherwise it is ignored.
    • vowelIndex() returns 0 to 4 for A, E, I, O, U and -1 for any other letter.
    • If p is 0 or more, freq[p] and vowels increase by 1; otherwise consonants increases by 1.
  4. display() prints the five vowel counts, Total vowels and Total consonants, or Empty line: nothing to count if str is empty.

Program

The class VowelFrequency gets the sentence through its constructor. toUpper(), vowelIndex(), count() and display() each do one job.

import java.util.Scanner;

/* Class VowelFrequency counts each vowel (A E I O U, either case) of a
   sentence given to the constructor, with the total vowels and consonants. */
public class VowelFrequency
{
    String str;        // the sentence to be analysed
    int freq[];        // freq[0..4] = counts of A, E, I, O, U
    int vowels;        // total number of vowels
    int consonants;    // total number of consonants

    /* Parameterised constructor. Parameter s: the sentence.
       Stores it and sets all counts to 0. */
    VowelFrequency(String s)
    {
        str = s;
        freq = new int[5];
        vowels = 0;
        consonants = 0;
    }

    /* Parameter c: a character. Returns c as a capital letter if it is a
       small letter (ASCII value reduced by 32), otherwise c unchanged. */
    char toUpper(char c)
    {
        if (c >= 'a' && c <= 'z')
            return (char)(c - 32);
        return c;
    }

    /* Parameter c: a capital letter. Returns its position 0 to 4 in
       A, E, I, O, U, or -1 if it is not a vowel. */
    int vowelIndex(char c)
    {
        if (c == 'A') return 0;
        if (c == 'E') return 1;
        if (c == 'I') return 2;
        if (c == 'O') return 3;
        if (c == 'U') return 4;
        return -1;
    }

    /* Scans str once: updates freq[], vowels and consonants */
    void count()
    {
        for (int i = 0; i < str.length(); i++)
        {
            char c = toUpper(str.charAt(i));      // case-insensitive comparison
            if (c >= 'A' && c <= 'Z')             // only letters are counted
            {
                int p = vowelIndex(c);
                if (p >= 0)                       // a vowel
                {
                    freq[p]++;
                    vowels++;
                }
                else                              // a letter that is not a vowel
                    consonants++;
            }
        }
    }

    /* Prints the frequency of all five vowels and the two totals;
       prints a message for an empty line */
    void display()
    {
        if (str.length() == 0)
        {
            System.out.println("Empty line: nothing to count");
            return;
        }
        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);
    }

    /* main reads the sentence, creates the object and calls the methods */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = sc.nextLine();
        VowelFrequency ob = new VowelFrequency(s);   // object of this class
        ob.count();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
strStringData member: the sentence to be analysed
freqint[]Data member: five counters for A, E, I, O, U
vowelsintData member: total number of vowels found
consonantsintData member: total number of consonants found
sStringParameter of the constructor, and the local in main holding the line typed by the user
ccharParameter of toUpper() and vowelIndex(); in count() the current character after conversion to a capital
iintLoop counter: position in str in count() and index into freq in display()
pintPosition 0 to 4 of the vowel in A, E, I, O, U, or -1 if the letter is not a vowel
vStringThe letters "AEIOU", used by display() to print the vowel names
scScannerReads the line from the keyboard in main
argsString[]Command line arguments of main (not used)
obVowelFrequencyObject created in main to call the methods

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!. The constructor sets freq to {0, 0, 0, 0, 0}, vowels and consonants to 0. Each row is one pass of the loop in count().

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

display() 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. 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."
  2. Replace vowels "Write a program to accept a word and replace every vowel with *."
  3. Vowel-bounded words "Write a program to accept a sentence and print the words that begin and end with a vowel."