Freedom
Freedom

Reputation: 347

Java Recursion - counting Characters in a string

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);
    }
}

Upvotes: 0

Views: 17865

Answers (5)

Mzf
Mzf

Reputation: 5260

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

Upvotes: 4

Sameer
Sameer

Reputation: 807

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.

Upvotes: 1

Patrick87
Patrick87

Reputation: 28292

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.

Upvotes: 2

G. Bach
G. Bach

Reputation: 3909

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);
    }
}

Upvotes: 1

rgettman
rgettman

Reputation: 178253

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.

Upvotes: 2

Related Questions