Java Program to Count the Presence of a Specific Letter in a String by Using Recursion

In the previous article, we have discussed about Java Program to Implement Linear Search by Using Recursion

In this program we are going to see how to count the presence of a specific letter in a String using Recursion by Java programming language.

Java Program to Count the Presence of a Specific Letter in a String by Using Recursion

Let’s start it with an example.

Assume there is a string 's' = “BtechGeeks is Best”
Number of times letter 'e' present in the string = 4

Now let’s see different ways to count the presence of a specific letter in a String by using recursion.

Method-1: Java Program to Count the Presence of a Specific Letter in a String By Using Static Input and Recursion

Approach:

  • Declare an string variable say ‘str’ and initialize.
  • Call a user defined method count() method and pass ‘str’ as parameter.
  • Inside the user defined method, check if the string is null then 0 is returned to the main() method else if the string is not null then compare the first index char to the specific char which we wanted to count. If the index char is equal to the specified char then increment the value of k by 1 and add the value of k with the recursive count()method as “k += count(str.substring(1))
  • Now, the value of count() method is stored in an integer variable say “count
  • Print the result.

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 = "Jack and Jill went upto the hill";
        // calling count() method with string “str” as parameter and storing the value in an integer variable “count”
        int count = count(str);
        // print the result
        System.out.println("There are " + count + " l's in the string '"+str+"'");  
    }
    
    // defining count() method to count specific letter in the string
    public static int count(String str) 
    {
        // if the string is null then return 0 to the main() method
        if(str.equals(""))
            return 0;
        // if the string is not null then compare the 1st index char to the specific char which we wanted to count. If the index char is equal to the specified char then increment the value of k by 1 and add the value of k with the recursive count()method as “k += count(str.substring(1))”
        else 
        {
            int k = 0;
            if(str.substring(0, 1).equals("l"))
                k++;
                k += count(str.substring(1));
                return k;
        }
    }
}
Output:

There are 4 l's in the string 'Jack and Jill went upto the hill'

Method-2: Java Program to Count the Presence of a Specific Letter in a String By Using User Input and Recursion

Approach:

  • Create a scanner class object.
  • Declare an string variable say ‘str’ and take the value as user input.
  • Call a user defined method count() method and pass ‘str’ as parameter.
  • Inside the user defined method, check if the string is null then 0 is returned to the main() method else if the string is not null then compare the first index char to the specific char which we wanted to count. If the index char is equal to the specified char then increment the value of k by 1 and add the value of k with the recursive count()method as “k += count(str.substring(1))
  • Now, the value of count() method is stored in an integer variable say “count
  • Print the result.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    static String s;
    public static void main(String[] args)
    {
        // creating scanner class
         Scanner sc = new Scanner(System.in);
        // prompting the user to enter a string value
        System.out.println("Enter a string");
        String str = sc.nextLine();
        // prompting the user to enter a specific letter to count in the string
        System.out.println("Enter a specific letter to count in the string");
        String str1 = sc.nextLine();
        s = str1;
        // calling count() method with string “str” as parameter and storing the value in an integer variable “count”
        int count = count(str);
        // print the result
        System.out.println("There are " + count + " " +str1+"'s in the string '"+str+"'");  
    }
    
    // defining count() method to count specific letter in the string
    public static int count(String str) 
    {
        // if the string is null then return 0 to the main() method
        if(str.equals(""))
            return 0;
        // if the string is not null then compare the 1st index char to the specific char which we wanted to count. If the index char is equal to the specified char then increment the value of k by 1 and add the value of k with the recursive count()method
        else 
        {
            int k = 0;
            if(str.substring(0, 1).equals(s))
                k++;
                k += count(str.substring(1));
                return k;
        }
    }
}
Output:

Enter a string
Jack and Jill went upto the hill
Enter a specific letter to count in the string
t
There are 3 t's in the string 'Jack and Jill went upto the hill'

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: