ISC Practicals and Theory - Count Vowels (Recursive Technique)
Updated 20 Oct 2026
This program counts how many vowels (a, e, i, o, u, in capital or small letters) a string contains. It uses recursion: the method countVow() examines the first character, then calls itself for the rest of the string.
Definition
The vowels are a, e, i, o, u and their capitals A, E, I, O, U. Every other character (consonants, spaces, digits, symbols) is not counted. The letter y is not a vowel here: it is treated as a consonant.
Computer Sciencehas 6 vowels:o,u,ein Computer andi,e,ein Science.Rhythm 123has 0 vowels.Yesterdayhas 3 vowels (e,e,a); the twoyletters are not counted.- The program reads a whole line, so an empty line is possible. For an empty line the base case returns 0 at once and the program prints
Number of vowels = 0(checked by running the program).
Algorithm
accept()reads a whole line (spaces allowed) intostr.display()callscountVow(str), stores the answer incountand prints it.countVow(s)works on the strings:- Base case: if
s.length()is 0, return 0. - If the first character
s.charAt(0)is a vowel (tested byisVowel()), return1 + countVow(s.substring(1)). - Recursive case: otherwise return
countVow(s.substring(1)), the count for the string without its first character.
- Base case: if
isVowel(c)returnstrueifcis one of the ten vowel characters, otherwisefalse.
Program
The class Vowels keeps the string and the count as data members. isVowel() tests one character, countVow() is the recursive method, and main creates the object and calls accept() and display().
import java.util.Scanner;
/* Class Vowels counts the vowels (a, e, i, o, u in either case) of a string
using recursion. */
public class Vowels
{
String str; // stores the string entered by the user
int count; // stores the number of vowels found
/* Default constructor: initialises the data members */
Vowels()
{
str = "";
count = 0;
}
/* Reads a line of text (it may contain spaces) and stores it in str */
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
str = sc.nextLine();
}
/* Parameter c: a character. Returns true if c is a vowel in either case,
otherwise false. */
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: no characters left
return 0;
if (isVowel(s.charAt(0))) // first character is a vowel
return 1 + countVow(s.substring(1));
return countVow(s.substring(1)); // not a vowel: count the rest only
}
/* Stores the vowel count in count and prints it */
void display()
{
count = countVow(str);
System.out.println("Number of vowels = " + count);
}
/* main creates the object and calls the methods */
public static void main(String args[])
{
Vowels ob = new Vowels(); // object of class Vowels
ob.accept();
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
str | String | Data member: the string entered by the user |
count | int | Data member: number of vowels found |
sc | Scanner | Reads input from the keyboard in accept() |
c | char | Parameter of isVowel(): the character being tested |
s | String | Parameter of countVow(): the part of the string still to be examined |
args | String[] | Command line arguments of main (not used) |
ob | Vowels | Object created in main to call the methods |
Sample input and output
Sample 1
Enter a string: Computer Science
Number of vowels = 6Sample 2
Enter a string: Rhythm 123
Number of vowels = 0Sample 3
Enter a string: AEIOU aeiou
Number of vowels = 10Dry run
Input Rhythm 123. The calls go down, one character removed each time, until the string is empty. No character is a vowel, so every call adds 0 on the way back.
| Call | s | First character | Vowel? | Returns |
|---|---|---|---|---|
| 1 | Rhythm 123 | R | no | countVow("hythm 123") |
| 2 | hythm 123 | h | no | countVow("ythm 123") |
| 3 | ythm 123 | y | no | countVow("thm 123") |
| 4 | thm 123 | t | no | countVow("hm 123") |
| 5 | hm 123 | h | no | countVow("m 123") |
| 6 | m 123 | m | no | countVow(" 123") |
| 7 | 123 | space | no | countVow("123") |
| 8 | 123 | 1 | no | countVow("23") |
| 9 | 23 | 2 | no | countVow("3") |
| 10 | 3 | 3 | no | countVow("") |
| 11 | empty | none | base case | 0 |
The 0 returns through calls 10 to 1 unchanged, so count = 0 and the program prints Number of vowels = 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."