Java Program to Find First Lowercase Letter in a String by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Convert String to Integer by Using Recursion

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

Java Program to Find First Lowercase Letter in a String by Using Recursion

Let’s understand it with an example.

Assume there is a string “I LOVe JaVa”

The first lowercase letter is 'e'

Now let’s see different ways to find first lowercase in a string by using recursion.

Method-1: Java Program to Find First Lowercase Letter in a String By Using Static Input and Recursion

Approach:

  • Declare and initialize an String variable ‘str’ as “I love JAVA”
  • Call a user defined method firstLowerCase() and pass the string ‘str’ and the 1st index ‘0’ as parameter.
  • Inside the user defined method, check if the 1st character is lowercase or not.
  • If the 1st char is lowercase then return that value else call the firstLowerCase() method recursively to find the first lowercase value.
  • If the string has no lowercase value then it throws an exception which is handled with an “exception occurs” message and then returns 0 to the main() method.
  • Now the value of the user defined method firstLowerCase() is stored in an integer variable say ‘b’ inside the main() method.
  • Print the value of first lowercase 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";
        System.out.println("The string is: "+str);
        //call the method and store the first lowercase value inside an integer variable say ‘b’
        char b = firstLowerCase(str,0);
        //print the result
        if (b == 0)
            System.out.println("The string has No lowercase letter");
        else
             System.out.println("The first lowercase in the string is: "+b);
    }
    
    // firstLowerCase() function is to find the first lowercase in the string
    static char firstLowerCase(String str, int n)
    {
        // checking if the 1st character is lowercase or not
        if(Character.isLowerCase(str.charAt(n))) 
        {
            return str.charAt(n);
        }
        //calling firstLowerCase() function recursively to find the first lowercase in the string
        try 
        {
            return firstLowerCase(str, n + 1);
        }
        // if there is no lowercase letter in the string then it throws an exception and return 0
        catch(Exception e)
        {
            System.out.println("Exception occurs ");
        }
        return 0;
    }
}
Output:

The string is: I LOVe Java
The first lowercase in the string is: e

Method-2: Java Program to Find First Lowercase Letter in a String By Using User Input and Recursion

Approach:

  • Declare and initialize an String variable ‘str’.
  • Prompt the user to enter the values for the string.
  • Call a user defined method firstLowerCase() and pass the string ‘str’ and the 1st index ‘0’ as parameter.
  • Inside the user defined method, check if the 1st character is lowercase or not.
  • If the 1st char is lowercase then return that value else call the firstLowerCase() method recursively to find the first lowercase value.
  • If the string has no lowercase value then it throws an exception which is handled with an “exception occurs” message and then returns 0 to the main() method.
  • Now the value of the user defined method firstLowerCase() is stored in an integer variable say ‘b’ inside the main() method.
  • Print the value of first lowercase 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();
        System.out.println("The string is: "+str);
        //define the method and store the first lowercase value inside an integer variable say ‘b’
        char b = firstLowerCase(str,0);
        //print the result
        if (b == 0)
            System.out.println("The string has No lowercase letter");
        else
            System.out.println("The first lowercase in the string is: "+b);
    }
    
    // firstLowerCase() function is called to find the first lowercase in the string
    static char firstLowerCase(String str, int n)
    {
        // checking if the 1st character is lowercase or not
        if(Character.isLowerCase(str.charAt(n))) 
        {
            return str.charAt(n);
        }
        //calling firstLowerCase() function recursively to find the first lowercase in the string
        try 
        {
            return firstLowerCase(str, n + 1);
        }
        // if there is no lowercase letter in the string then it throws an exception and return 0
        catch(Exception e)
        {
            System.out.println("Exception occurs ");
        }
        return 0;
    }
}
Output:

Enter a string value: 
BTECHGEEKS
The string is: BTECHGEEKS
Exception occurs 
The string has No lowercase letter

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: