Question: Consider the following program that counts consonants in a string: import java.io.*; class GFG { // Function to check for consonant static boolean isConsonant(char ch)
Consider the following program that counts consonants in a string:
import java.io.*; class GFG {
// Function to check for consonant static boolean isConsonant(char ch) {
// To handle lower case ch = Character.toUpperCase(ch);
return !(ch == 'A' || ch == 'E' || ch == 'I'|| ch == 'O'||
ch == 'U') && ch >= 65 && ch <= 90;
}
static int totalConsonants(String str) {
int count = 0; for (int i = 0; i < str.length(); i++)
// To check is character is Consonant if (isConsonant(str.charAt(i)))
++count; return count;
}
// Driver code public static void main(String args[]) {
String str = "abc de";
System.out.println( totalConsonants(str)); }
}
Rewrite totalConsonants method in a recursive way.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
