Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program that counts consonants in a string: import java.i

ID: 3907331 • Letter: C

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) {

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[]) {

System.out.println( totalConsonants(str)); }

}


Rewrite totalConsonants method in a recursive way.

Explanation / Answer

import java.io.*; class GFG { 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