ISC Practicals and Theory - Count Vowels (Recursive Technique) (OOP)
Updated 20 Oct 2026
This version counts the vowels (a, e, i, o, u in either case) of a string using recursion, written as a class built with a parameterised constructor. The string is passed to the object when it is created, and each method does one job: test a character, count recursively, store, print.
Definition
The vowels are a, e, i, o, u and A, E, I, O, U. Everything else (consonants including y, spaces, digits) is not counted. For example Programming Language has 7 vowels: o, a, i in Programming and a, u, a, e in Language, and Yesterday has 3 (e, e, a) because y is not a vowel. The line is read with nextLine(), so an empty line is possible: the base case returns 0 at once and the program prints Number of vowels in "" = 0 (checked by running the program).
Algorithm
mainreads a line and createsobwithnew VowelsOOP(s). The constructor stores the string intextand setscountto 0.find()callscountVow(text)and stores the answer incount.countVow(s)is the recursive method:- Base case: if
s.length()is 0, return 0. - Set
addto 1 ifisVowel(s.charAt(0))is true, otherwise 0. - Recursive case: return
add + countVow(s.substring(1)).
- Base case: if
isVowel(c)returnstruefor the ten vowel characters.display()prints the string andcount.
Program
The class VowelsOOP has two data members, a parameterised constructor, isVowel(), the recursive countVow(), and the one-job methods find() and display().
import java.util.Scanner;
/* Class VowelsOOP counts the vowels of a string recursively. The string is
given to the constructor when the object is created. */
public class VowelsOOP
{
String text; // the string whose vowels are to be counted
int count; // number of vowels found in text
/* Parameterised constructor. Parameter s: the string typed by the user. */
VowelsOOP(String s)
{
text = s;
count = 0;
}
/* Parameter c: a character. Returns true if c is a vowel (either case). */
boolean isVowel(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
/* Recursive method. Parameter s: the part of the string still to be examined.
Returns the number of vowels in s. */
int countVow(String s)
{
if (s.length() == 0) // base case: empty string
return 0;
int add = 0; // 1 if the first character is a vowel
if (isVowel(s.charAt(0)))
add = 1;
return add + countVow(s.substring(1)); // recursive case: rest of the string
}
/* Counts the vowels of text and stores the answer in count */
void find()
{
count = countVow(text);
}
/* Prints the string and its vowel count */
void display()
{
System.out.println("Number of vowels in \"" + text + "\" = " + count);
}
/* main reads the string, creates the object and calls the methods */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter a string: ");
String s = sc.nextLine(); // line typed by the user
VowelsOOP ob = new VowelsOOP(s); // object of the class
ob.find();
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
text | String | Data member: the string whose vowels are counted |
count | int | Data member: number of vowels found |
s | String | Parameter of the constructor and of countVow(); also the line read in main |
c | char | Parameter of isVowel(): the character being tested |
add | int | Local in countVow(): 1 if the first character is a vowel, else 0 |
sc | Scanner | Reads input from the keyboard in main |
args | String[] | Command line arguments of main (not used) |
ob | VowelsOOP | Object created in main to call the methods |
Sample input and output
Sample 1
Enter a string: Rhythm 123
Number of vowels in "Rhythm 123" = 0Sample 2
Enter a string: Programming Language
Number of vowels in "Programming Language" = 7Sample 3
Enter a string: Sky
Number of vowels in "Sky" = 0Dry run
Input Sky, which has no vowels. The calls go down, then the values come back up.
| Call | s | First character | add | Returns |
|---|---|---|---|---|
| 1 | Sky | S | 0 | 0 + countVow("ky") |
| 2 | ky | k | 0 | 0 + countVow("y") |
| 3 | y | y | 0 | 0 + countVow("") |
| 4 | empty | none | not used | 0 (base case) |
Call 3 returns 0, call 2 returns 0, call 1 returns 0. So count = 0 and the program prints Number of vowels in "Sky" = 0, the same as the real run above.
Variations
- Count consonants "Write a program to count the consonants of a string using recursive technique."
- Count words "Write a program to count the number of words in a sentence."