public static boolean work(String str, char ch)
Use recursion to find out if str contains at least one occurrence of ch.
Return true if it does and false if not.
Example
work("whynot", 'n') returns true
work("please", 'z') returns false
public static boolean work(String str, char ch){
//base case
if (str == null || str.equals("")){
return false;
}
//recursive case
if (ch == str.charAt(0)){
return true;
}else {
work(str.substring(1), ch);
}
return false;
}
My code will correctly return "true" when ch is the first character of str, but return an incorrect answer of "false" when ch is in any other part of the str.
Please explain why... I was thinking it was because my last "return false;" statement overrode the "true" from the recursive case if, but when I get rid of the last "return false;" my compiler will complain that I'm missing a return value.