Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find First Lowercase Letter in a String by Using Recursion
In this program we are going to see how to find first uppercase in a string using by recursion in Java programming language.
Java Program to Find First Uppercase Letter in a String by Using Recursion
Let’s see an example to understand it.
Lets assume there is a string “i LoVe JaVa” The first uppercase letter is 'L'
Now let’s see different ways to find first uppercase in a string by using recursion.
Method-1: Java Program to Find First Uppercase Letter in a String By Using Static Input and Recursion
Approach:
- Declare and initialize a String variable ‘
str
’ as “i LoVe JaVa” - Call a user defined method
firstuppercase()
and pass the string ‘str
’ and the 1st index ‘0
’ as parameter. - Inside the user defined method we will check if the first character is uppercase or not.
- If the 1st char is uppercase then return that value else call the
firstuppercase()
method recursively to find the first uppercase value. - If the string has no uppercase 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
firstuppercase()
is stored in an integer variable say ‘b
’ inside the main() method. - Print the value of first uppercase 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); //define the method and store the first uppercase value inside an integer variable say ‘b’ char b = firstuppercase(str,0); //print the result if (b == 0) System.out.println("The string has No uppercase letter"); else System.out.println("The first uppercase in the string is: "+b); } // firstuppercase() function is called to find the first uppercase in the string static char firstuppercase(String str, int n) { // checking if the 1st character is uppercase or not if(Character.isUpperCase(str.charAt(n))) { return str.charAt(n); } //calling firstuppercase() function recursively to find the first uppercase in the string try { return firstuppercase(str, n + 1); } // if there is no uppercase 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 uppercase in the string is: L
Method-2: Java Program to Find First Uppercase Letter 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
firstuppercase()
and pass the string ‘str
’ and the 1st index ‘0
’ as parameter. - Inside the user defined method we will check if the 1st character is uppercase or not.
- If the 1st char is uppercase then return that value else call the
firstuppercase()
method recursively to find the first uppercase value. - If the string has no uppercase 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
firstuppercase()
is stored in an integer variable say ‘b
’ inside the main() method. - Print the value of first uppercase 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 uppercase value inside an integer variable say ‘b’ char b = firstuppercase(str,0); //print the result if (b == 0) System.out.println("The string has No uppercase letter"); else System.out.println("The first uppercase in the string is: "+b); } // firstuppercase() function is called to find the first uppercase in the string static char firstuppercase(String str, int n) { // checking if the 1st character is uppercase or not if(Character.isUpperCase(str.charAt(n))) { return str.charAt(n); } //calling firstuppercase() function recursively to find the first uppercase in the string try { return firstuppercase(str, n + 1); } // if there is no uppercase 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 uppercase letter
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Programs: