Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Using recursion to find the number of occurences of a character in a string

1. ## Using recursion to find the number of occurences of a character in a string

Having a bit of a problem with the recursive part of this case.

here's what I have so far :
``` public int freq (String S, Char C) {
if (S.length () == 0) return (0); // base case
else return // ....```

having trouble with the recursive case, I get that it will need to use the substring method, what i'm not sure of is if it needs a counter to count how man times the character occurs in the string. Any help appreciated.

2. ## The Following User Says Thank You to sean1604 For This Useful Post:

thomas10 (June 7th, 2013)

3. ## Re: Using recursion to find the number of occurences of a character in a string

Then check to see if the first character of the string is the passed in character. If so, return one plus the results of the freq() with the first character removed from the string. Otherwise, just return the results of freq() with the first character removed from the string.

```public static int freq(String s, char c) {
if (s.length() == 0)
return 0;
else if (s.charAt(0) == c)
return 1 + freq(s.substring(1, s.length()), c);
else
return freq(s.substring(1, s.length()), c);
}```

For example:

If you call freq("literallyjer" 'l') then you would get 1 + 1 + 1 + 0 = 3.

4. ## The Following 2 Users Say Thank You to literallyjer For This Useful Post:

sean1604 (October 22nd, 2009), thomas10 (June 7th, 2013)

5. ## Re: Using recursion to find the number of occurences of a character in a string

thanks for the reply. I'm just starting out with recursion and was wondering if you could give me a bit more info on this part of the code:

`return 1 + freq(s.substring(1, s.length()), c);`
I get that recursive methods need to call themselves, i don;t understand how the first letter is removied in this case though and why you need the +1, thanks again.

6. ## The Following User Says Thank You to sean1604 For This Useful Post:

thomas10 (June 7th, 2013)

7. ## Re: Using recursion to find the number of occurences of a character in a string

Look up the String.substring(int, int) method in the Java API.

```String s1 = "literally";
String s2 = s1.substring(0, 7);
System.out.println(s2) // prints: literal```

In our case, the substring method returns all the characters in the string starting from the second character (remember: counting indexes starts at 0) all the way the last character in the string.

```String s1 = "literally";
String s2 = s1.substring(1, s1.length());
System.out.println(s2) // prints: iterally```

Now, since our freq() method returns an integer, if the first character of the string is the same as our test character, then we want to return that integer plus one. If not, we just want to return whatever the next call to freq() returns.

A simple example: freq("dad", 'd')

```freq is called with "dad" and 'd'
is "dad" empty? no
does the first letter in "dad" equal 'd'? yes
add 1 to the next call to freq
freq is called with "ad" and 'a'
is "ad" empty? no
does the first letter in "ad" equal 'd'? no
freq is called with "d" and 'd'
is "d" empty? no
does the first letter of "d" equal 'd'? yes
add 1 to the next call of freq
freq is called with "" and 'd'
is "" empty? yes
return 0
return 1 + 0
return 1 + 1 + 0
return 1 + 1 + 0```

I hope that isn't too hard to follow.

8. ## The Following User Says Thank You to literallyjer For This Useful Post:

thomas10 (June 7th, 2013)

9. ## Re: Using recursion to find the number of occurences of a character in a string

What is you used s.isEmpty() instead of the first condition"(s.length() == 0)" will it be he same?

10. ## The Following User Says Thank You to barqims For This Useful Post:

thomas10 (June 7th, 2013)

11. ## Re: Using recursion to find the number of occurences of a character in a string

This thread is over 3 years old.
Thread closed.

12. ## The Following User Says Thank You to Norm For This Useful Post:

thomas10 (June 7th, 2013)