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

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 Science has 6 vowels: o, u, e in Computer and i, e, e in Science.
  • Rhythm 123 has 0 vowels.
  • Yesterday has 3 vowels (e, e, a); the two y letters 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

  1. accept() reads a whole line (spaces allowed) into str.
  2. display() calls countVow(str), stores the answer in count and prints it.
  3. countVow(s) works on the string s:
    • Base case: if s.length() is 0, return 0.
    • If the first character s.charAt(0) is a vowel (tested by isVowel()), return 1 + countVow(s.substring(1)).
    • Recursive case: otherwise return countVow(s.substring(1)), the count for the string without its first character.
  4. isVowel(c) returns true if c is one of the ten vowel characters, otherwise false.

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

VariableData TypePurpose
strStringData member: the string entered by the user
countintData member: number of vowels found
scScannerReads input from the keyboard in accept()
ccharParameter of isVowel(): the character being tested
sStringParameter of countVow(): the part of the string still to be examined
argsString[]Command line arguments of main (not used)
obVowelsObject created in main to call the methods

Sample input and output

Sample 1

Enter a string: Computer Science
Number of vowels = 6

Sample 2

Enter a string: Rhythm 123
Number of vowels = 0

Sample 3

Enter a string: AEIOU aeiou
Number of vowels = 10

One value per line

Dry 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.

CallsFirst characterVowel?Returns
1Rhythm 123RnocountVow("hythm 123")
2hythm 123hnocountVow("ythm 123")
3ythm 123ynocountVow("thm 123")
4thm 123tnocountVow("hm 123")
5hm 123hnocountVow("m 123")
6m 123mnocountVow(" 123")
7123spacenocountVow("123")
81231nocountVow("23")
9232nocountVow("3")
1033nocountVow("")
11emptynonebase case0

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

  1. Count consonants "Write a program to count the consonants of a string using recursive technique."
  2. Count words "Write a program to count the number of words in a sentence."