Question

Lab Goal : This lab was designed to teach you more about recursion. Lab Description : Take a string and remove all occ...


Lab Goal : This lab was designed to teach you more about recursion.


Lab Description : Take a string and remove all occurrences of the word chicken and count how many
chickens were removed. Keep in mind that removing a chicken might show a previously hidden chicken. You
may find substring and indexOf useful.


achickchickenen - removing the 1st chicken would leave achicken behind
achicken - removing the 2nd chicken would leave a behind
Sample Data :

itatfun
itatchickenfun
chchickchickenenicken
chickchickfun
chickenbouncetheballchicken
Sample Output :

0
1
3
0
2

Answer

=

public class RecursionFunTwo {

    public static int countChickens(String word) {
        if (word.isEmpty()) {
            return 0;
        } else {
            int count = countChickens(word.substring(1));
            if (word.startsWith("chicken")) {
                ++count;
            }
            return count;
        }
    }
}
Recent Questions