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

• October 22nd, 2009, 04:27 PM
sean1604
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 :
Code :

``` 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.
• October 22nd, 2009, 06:10 PM
literallyjer
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.

Code :

```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.
• October 22nd, 2009, 06:16 PM
sean1604
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:

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.
• October 22nd, 2009, 06:39 PM
literallyjer
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.

Code :

```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.

Code :

```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.

Code :

```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.
• April 22nd, 2013, 07:18 AM
barqims
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?
• April 22nd, 2013, 07:27 AM
Norm
Re: Using recursion to find the number of occurences of a character in a string
This thread is over 3 years old.