Write a recursive method that returns the amount of uppercas
Write a recursive method that returns the amount of uppercase letters that are in a string and that has the following method signature. (Hint: you may find the isUpperCase(char ch) method of the Character class helpful):
Solution
import java.util.*;
public class HelloWorld{
//recursive function countUppercaseLetters
public static int countUppercaseLetters(String s){
//if entered string is null
if (s.length() == 0) return 0;
//base case
int count = Character.isUpperCase(s.charAt(0)) ? 1 : 0;
//calling function recursively
return countUppercaseLetters(s.substring(1)) + count;
}
public static void main(String []args){
//variable to store string
String s;
Scanner input=new Scanner(System.in);
//program asking user to enter the string
System.out.println(\"Enter String :\");
s=input.nextLine();
//calling recursive function countUppercaseLetters and storing the returned value in count
int count=countUppercaseLetters(s);
//printing the output
System.out.println(\"The amount of uppercase letters in the string entered by the user :\"+count);
}
}
/******OUTPUT******
Enter String :
hi My namE I
The amount of uppercase letters in the string entered by the user :3
******OUTPUT********/
/* Note:this code has been tested on eclipse compiler,please ask in case of any doubt,Thanks. */
