Show the Java code for a recursive method int count String t
Solution
Hi, Please find my working implementation.
Please let me know in case of any issue.
public int count(String text, char c){
// if text is null
if(text == null)
return 0;
// if test has only one character
if(text.length() == 1){
if(text.charAt(0) == c)
return 1;
else
return 0;
}
// if current character is equal to c then add 1 and call for remaining character
if(text.charAt(0) == c)
return (1 + count(text.substring(1), c));
else
return count(text.substring(1), c);
}
public static int arraySum(int[] a){
// base case
if(a == null)
return 0;
// if array has only one element
if(a.length == 1)
return a[0];
return a[0] + arraySum(Arrays.copyOfRange(a, 1, a.length));
}

