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()orCharacter.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
mainreads the whole line intosand creates the objectobwithnew VowelFrequency(s).- The constructor stores
sinstr, createsfreqwith five elements and setsvowelsandconsonantsto 0. count()visits each positioniofstr: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
pis 0 or more,freq[p]andvowelsincrease by 1; otherwiseconsonantsincreases by 1.
display()prints the five vowel counts,Total vowelsandTotal consonants, orEmpty line: nothing to countifstris 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
| Variable | Data Type | Purpose |
|---|---|---|
str | String | Data member: the sentence to be analysed |
freq | int[] | Data member: five counters for A, E, I, O, U |
vowels | int | Data member: total number of vowels found |
consonants | int | Data member: total number of consonants found |
s | String | Parameter of the constructor, and the local in main holding the line typed by the user |
c | char | Parameter of toUpper() and vowelIndex(); in count() the current character after conversion to a capital |
i | int | Loop counter: position in str in count() and index into freq in display() |
p | int | Position 0 to 4 of the vowel in A, E, I, O, U, or -1 if the letter is not a vowel |
v | String | The letters "AEIOU", used by display() to print the vowel names |
sc | Scanner | Reads the line from the keyboard in main |
args | String[] | Command line arguments of main (not used) |
ob | VowelFrequency | Object 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 = 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!. 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().
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 |
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
- 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."