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 123has 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 andfreq[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
- Read the sentence into
str. Create the arrayfreqof size 5 (the counts of A, E, I, O, U) and setvowels = 0andconsonants = 0. - For every character
cof 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. - If
cis now a letter (between'A'and'Z'), findp, its index inA, E, I, O, U, or -1 if it is not a vowel.- If
p >= 0, add 1 tofreq[p]and tovowels. - Otherwise it is a consonant: add 1 to
consonants.
- If
- Digits, spaces and punctuation marks are not letters, so they are ignored.
- If the line was empty, print a message. Otherwise print the count of each vowel,
vowelsandconsonants.
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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
str | String | The sentence typed by the user |
freq | int[] | freq[0] to freq[4] count the vowels A, E, I, O, U |
vowels | int | Total number of vowels |
consonants | int | Total number of consonants |
i | int | Loop counter over the characters, then over the five vowels |
c | char | The character being examined, changed to a capital if it is a small letter |
p | int | Index of the vowel in A, E, I, O, U; -1 if the letter is not a vowel |
v | String | The 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 = 9Sample 2
Enter a sentence: Rhythm myth 123
A = 0
E = 0
I = 0
O = 0
U = 0
Total vowels = 0
Total consonants = 10Sample 3
Enter a sentence: AEIOU aeiou Uu
A = 2
E = 2
I = 2
O = 2
U = 4
Total vowels = 12
Total consonants = 0Sample 4
Enter a sentence: Bee Eat!
A = 1
E = 3
I = 0
O = 0
U = 0
Total vowels = 4
Total consonants = 2Dry 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.
i | str.charAt(i) | c | p | Action | freq (A E I O U) | vowels | consonants |
|---|---|---|---|---|---|---|---|
| 0 | B | B | -1 | consonant | 0 0 0 0 0 | 0 | 1 |
| 1 | e | E | 1 | vowel | 0 1 0 0 0 | 1 | 1 |
| 2 | e | E | 1 | vowel | 0 2 0 0 0 | 2 | 1 |
| 3 | space | space | not a letter | ignored | 0 2 0 0 0 | 2 | 1 |
| 4 | E | E | 1 | vowel | 0 3 0 0 0 | 3 | 1 |
| 5 | a | A | 0 | vowel | 1 3 0 0 0 | 4 | 1 |
| 6 | t | T | -1 | consonant | 1 3 0 0 0 | 4 | 2 |
| 7 | ! | ! | not a letter | ignored | 1 3 0 0 0 | 4 | 2 |
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
- Count vowels using recursion "Write a program to count the number of vowels in a string using recursive technique." See Count Vowels.
- 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."
- Replace vowels "Write a program to accept a word and replace every vowel with
*." - Vowel-bounded words "Write a program to accept a sentence and print the words that begin and end with a vowel."