Java Program to Find Frequency of Vowels in a String Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Implement Ackerman Function by Using recursion

In this program we are going to see how to find frequency of vowels in a string by using recursion in Java programming language.

Java Program to Find Frequency of Vowels in a String Using Recursion

There are 5 vowels in English alphabets i.e (a,e,i,o,u) or (A,E,I,O,U)

Let assume a string “I love JAVA”

Vowels in this string = {I, o, e, A, A}

Frequency of vowels in this string = 5

Now let’s see different ways to find frequency of vowels in a string by using Recursion.

Method-1: Java Program to Find Frequency of Vowels in a String By Using Static Input and Recursion

Approach:

  • Declare and initiate a String variable ‘str’ as “I love JAVA”
  • Call a user defined method countVowels() and pass the string ‘str’ and the length of the string ‘str.length()’ as parameter.
  • Inside the user defined method we will call isVowel() Function to check if the character is Vowel or not then return the value to the countVowel() method where the vowels are counted recursively to count total number of vowel from 0 to n and then the value is returned to the main() method.
  • Now the value of the user defined method countVowels() is stored in an integer variable say ‘b’ inside the main() method.
  • Print the value of frequency of vowels in that string.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        //declare and initialize an String variable str
        String str = "I love JAVA";
        //define the method and store the frequency of vowels value inside an integer variable say ‘b’
        int b = countVowels(str, str.length());
        //print the result
        System.out.println("The frequency of vowels in the string of ‘"+str+"’ is: "+b);
    }
    
    // countVowels() function is called to count total number of vowel from 0 to n(i.e. length of string)
    static int countVowels(String str, int n)
    {
        if (n == 1)
            return isVowel(str.charAt(n - 1));
            return countVowels(str, n-1) + isVowel(str.charAt(n - 1));
    }
    
    // isVowel() Function is called to check the character is Vowel or not
    static int isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
        if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
           return 1;
        else return 0;
    }
}
Output:

The frequency of vowels in the string of ‘I love JAVA’ is: 5

Method-2: Java Program to Find Frequency of Vowels in a String By Using User Input and Recursion

Approach:

  • Create a scanner class.
  • Declare a string variables say ‘str’
  • Prompt the user to enter the values for the string.
  • Call a user defined method countVowels() and pass the string ‘str’and the length of the string ‘str.length()’ as parameter.
  • Inside the user defined method we will call isVowel() Function to check if the character is Vowel or not then return the value to the countVowel() method where the vowels are counted recursively to count total number of vowel from 0 to n and then the value is returned to the main() method.
  • Now the value of the user defined method countVowels() is stored in an integer variable say ‘b’ inside the main() method.
  • Print the value of frequency of vowels in that string.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a string value: ");
        //declare an integer variable ‘n’and initialize it by user input using scanner class.
        String str = s.nextLine();
        //define the method and store the frequency of vowels value inside an integer variable say ‘b’
        int b = countVowels(str, str.length());
        //print the result
        System.out.println("The frequency of vowels in the string of ‘"+str+"’ is: "+b);
    }
    
    // countVowels() function is called to count total number of vowel from 0 to n(i.e. length of string)
    static int countVowels(String str, int n)
    {
        if (n == 1)
            return isVowel(str.charAt(n - 1));
            return countVowels(str, n-1) + isVowel(str.charAt(n - 1));
    }
    
    // isVowel() Function is called to check the character is Vowel or not
    static int isVowel(char ch)
    {
        ch = Character.toUpperCase(ch);
        if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
           return 1;
        else return 0;
    }
}
Output:

Enter a string value: 
I love BtechGeeks
The frequency of vowels in the string of ‘I love BtechGeeks’ is: 6

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: