0

I'm trying to find the number of occurrences "character" is found in "str" using recursion. I think I have the concept of what to do but for some reason the code does not work when I test it out...do you know why its wrong?

   public static int countChar(String str, String character) {
    int number = 0;
    if(str.length()==1) {
        return number;
    }
    if (!(str.substring(0,1).equals(character))) {
        return countChar(str.substring(1), character);
    } else {
        number = number + 1;
        return countChar(str.substring(1), character);
    }
}
4
  • "Does not work", can you give more details? Expected output, actual output, errors, etc. Commented Mar 24, 2014 at 17:05
  • In what way does it not work? With what input, what is the expected and observed output? Commented Mar 24, 2014 at 17:05
  • Any reason you're doing this recursively? You can just do a simple loop...recursion doesn't offer too much advantage here. Commented Mar 24, 2014 at 17:06
  • Sorry, I was a bit unclear. Basically it outputted 0 instead of whatever number it was supposed to output. Commented Mar 24, 2014 at 17:07

5 Answers 5

5

number is a local variable ....

 public static int countChar(String str, String character) {
    if(str.length()==0) {
        return 0;
    }

    if ((str.substring(0,1).equals(character))) {
        return 1 + countChar(str.substring(1), character);
    }

    return countChar(str.substring(1), character);
}

The terminating case is when the String length is zero.

For each step , check the current char , if match - add 1 to the result for the rest of the string, if not return the match result for the rest of the string

Sign up to request clarification or add additional context in comments.

Comments

2

In the else case, number is ignored after it's incremented. But it's not needed anyway. Just add one to whatever the recursive call returns.

} else {
    return 1 + countChar(str.substring(1), character);
}

Also, your base case should be if the string is empty, with a length of 0 returning 0.

Comments

2

A few things:

  1. The base case should probably be str.length() == 0, not str.length() == 1. While there's no right or wrong base case, it's easier here to get the right behavior for an empty string. Your behavior in the base case of length 1 strings is actually wrong; what if the length 1 string does contain character? Then you're missing it.

  2. Your first if looks good; if it doesn't match the first character, return the result of countChar applied to the rest of the string.

  3. Your second if isn't quite right; you want to return 1 plus the result of countChar applied to the rest of the string.

It looks like you've got one misconception that's making this harder than it needs to be: the way the code is written, you think that number retains its value in recursive calls. This isn't the case; every time you go into a new recursive call, the value of number is reset to 0. number is local to the function, which means that each recursive call gets its own copy to play with. If you want the recursive calls to get a value like that, you need to pass it as an argument, like you're doing with substrings.

Comments

1

Code for what rgettman specified:

public static int countChar(String str, String character) {

    if(str.length() == 0) {
        return 0;
    }

    if (!(str.substring(0,1).equals(character))) {
        return countChar(str.substring(1), character);
    } else {
        return 1 + countChar(str.substring(1), character);
    }
}

Comments

1

First and foremost

int number = 0;

Number is getting initialized to 0 on each call. Make it global or pass the value as a parameter in each call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.