Java Program to Find Frequency of Consonants in String by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Minimum Value in Array by Using Recursion

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

Java Program to Find Frequency of Consonants in String by Using Recursion

There are 5 vowels out of 26 alphabets i.e (a,e,i,o,u) and the remaining letters are consonants.

No. Of consonants = 26-5 = 21

Example-

Let assume a string “I love JAVA”
Consonants in this string = {l, v, J, V}
Frequency of consonants in this string = 4

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

Note- Take input of character/string which must be set of alphabets.

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

Approach:

  • Declare and initialize a String variable ‘str’ as “I love JAVA”
  • Call a user defined method countConsonants() and pass the string ‘str’ and the length of the string ‘str.length()’ as parameter.
  • Inside the user defined method we will call isConsonant() Function to check if the character is consonant or not then return the value to the countConsonants() method where the consonants are counted recursively to count total number of consonants from 0 to n and then the value is returned to the main() method.
  • Now the value of the user defined method countConsonants() is stored in an integer variable say ‘b’ inside the main() method.
  • Print the value of frequency of consonants 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 consonants value inside an integer variable say ‘b’
        int b = countConsonants(str, str.length());
        //print the result
        System.out.println("The frequency of consonants in the string of ‘"+str+"’ is: "+b);
    }
    
    // countconsonants() function is called to count total number of consonants from 0 to n
    static int countConsonants(String str, int n)
    {
        if (n == 1)
            return isConsonant(str.charAt(n - 1));
        return countConsonants(str, n-1) + isConsonant(str.charAt(n - 1));
    }
    
    // isConsonant() Function is called to check the character is consonant or not
    static int isConsonant(char ch)
    {
        ch = Character.toUpperCase(ch);
        if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch==' ')
            return 0;
        else
            return 1;
    }
}
Output:

The frequency of consonants in the string of ‘I love JAVA’ is: 4

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

Approach:

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

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 ‘str’and initialize it by user input using scanner class.
        String str = s.nextLine();
        //define the method and store the frequency of consonants value inside an integer variable say ‘b’
        int b = countConsonants(str, str.length());
        //print the result
        System.out.println("The frequency of consonants in the string of ‘"+str+"’ is: "+b);
    }
    
    // countconsonants() function is called to count total number of consonant from 0 to n
    static int countConsonants(String str, int n)
    {
        if (n == 1)
            return isConsonant(str.charAt(n - 1));
        return countConsonants(str, n-1) + isConsonant(str.charAt(n - 1));
    }
    
    // isConsonant() Function is called to check the character is consonant or not
    static int isConsonant(char ch)
    {
        ch = Character.toUpperCase(ch);
        if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch==' ')
            return 0;
        else
            return 1;
    }
}
Output:

Enter a string value: 
BTechGeeks is Best
The frequency of consonants in the string of ‘BTechGeeks is Best’ is: 11

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: