7 Replies Latest reply: Jun 28, 2008 12:49 PM by 796440

# recursive method

I need help trying to write a recursive method returning the number of occurrences of a specified character in the HTML String.
• ###### 1. Re: recursive method
And...?
• ###### 2. Re: recursive method
SaintNicholas wrote:
I need help trying to write a recursive method returning the number of occurrences of a specified character in the HTML String.
Do you really want a stack depth equal to O(n) where n is the number of characters in your string?

kind regards,

Jos
• ###### 3. Re: recursive method
SaintNicholas wrote:
I need help trying to write a recursive method returning the number of occurrences of a specified character in the HTML String.

• ###### 4. Re: recursive method
what i meant was; what recursive method would u use to return number of occurrence of a particular character in a word.
• ###### 5. Re: recursive method
SaintNicholas wrote:
what i meant was; what recursive method would u use to return number of occurrence of a particular character in a word.
Well, you got one suggestion in the other thread. Have you considered it?

The big question is - why use recursion at all?

If I had to use recursion I would worry about how long a word can be. If it's like thousands of character I would base recursion on a divide-and-conquer approach to keep the stack requirements down to log N (where N is the number of characters).
• ###### 6. Re: recursive method
The only time I'd ever use a recursive method to find the number of occurrences of a word in a string is in LISP or Scheme.
``````(define (count-words-flat str word)
(cond
((null? str) 0)
((eq? (car str) word) (+ 1 (count-words-flat (cdr str) word))
(else (count-words-flat (cdr str) word))
)
)``````
• ###### 7. Re: recursive method
SaintNicholas wrote:
what i meant was; what recursive method would u use to return number of occurrence of a particular character in a word.
You would use the method that you write yourself, posting here for specific questions when you get stuck.