forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise18_14.java
30 lines (26 loc) · 922 Bytes
/
Exercise18_14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package ch_18;
import java.util.Scanner;
/**
* *18.14 (Find the number of uppercase letters in a string)
* Write a recursive method to return the number of uppercase letters in a string.
* <p>
* Write a test program that prompts the user to enter a string and displays the number of uppercase letters in
* the string.
*/
public class Exercise18_14 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a String: ");
String s = in.nextLine().trim();
System.out.println("The number of uppercase letters in " + s + " is: " + numUpperCase(s, 0, 0));
}
static int numUpperCase(String str, int idx, int count) {
if (idx == str.length()) {
return count;
}
if (Character.isUpperCase(str.charAt(idx))) {
count++;
}
return numUpperCase(str, ++idx, count);
}
}