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

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

  1. main reads a line and creates ob with new VowelsOOP(s). The constructor stores the string in text and sets count to 0.
  2. find() calls countVow(text) and stores the answer in count.
  3. countVow(s) is the recursive method:
    • Base case: if s.length() is 0, return 0.
    • Set add to 1 if isVowel(s.charAt(0)) is true, otherwise 0.
    • Recursive case: return add + countVow(s.substring(1)).
  4. isVowel(c) returns true for the ten vowel characters.
  5. display() prints the string and count.

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

VariableData TypePurpose
textStringData member: the string whose vowels are counted
countintData member: number of vowels found
sStringParameter of the constructor and of countVow(); also the line read in main
ccharParameter of isVowel(): the character being tested
addintLocal in countVow(): 1 if the first character is a vowel, else 0
scScannerReads input from the keyboard in main
argsString[]Command line arguments of main (not used)
obVowelsOOPObject created in main to call the methods

Sample input and output

Sample 1

Enter a string: Rhythm 123
Number of vowels in "Rhythm 123" = 0

Sample 2

Enter a string: Programming Language
Number of vowels in "Programming Language" = 7

Sample 3

Enter a string: Sky
Number of vowels in "Sky" = 0

One value per line

Dry run

Input Sky, which has no vowels. The calls go down, then the values come back up.

CallsFirst characteraddReturns
1SkyS00 + countVow("ky")
2kyk00 + countVow("y")
3yy00 + countVow("")
4emptynonenot used0 (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

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