lundi 25 novembre 2019

How do I make this function return the value I'm looking for?

public static int countCharInString(String s, char c)
    {

        return countCharInString(s, c, 0);


    }


    public static int countCharInString(String s, char c, int index)
    {
        if(index==s.length())
        {
            return 0;
        }
        if(s.charAt(index) == c)
        {
            return 1 + countCharInString(s, c, index+1);
        }
        if(s.charAt(index)!=c)
        {
            return countCharInString(s, c, index+1);
        }

    }

I made a function that's meant to count the number of specific chars in a function recursively. How can I put a return statement at the end of the function that'll return the whole number I "counted" inside the function?

Aucun commentaire:

Enregistrer un commentaire