I have looked at some of the answers here, they are either too complicated for your question, use substring, or do not use basic recursion
your solution should be something like this.
function countChar(String a, char b, int i)
if(i==a.length)
return 0
else if(b ==a.charAt(i))
return 1 + function countChar(a, b, i+1)
else
return function countChar(a,b,i+1)
This part of the code is the loop.
function countChar(String a, char b, int i)
{
if(i==a.length)
{
return 0
}
else
{
//put here a print statement to see what happens.
// print charAt(i) or i itself
return function countChar(a, b, i+1) //<-- moving to the next index
}
}
Learn that loop. You should use charAt(i). Substring is not neccesssary..
In order to fulfil the requirement, you can wrap your method in another recursive one.
function howManyChar(String a, Char b)
{
countChar(a, b, 0);
}