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

    recursive method

    807589
      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
          796440
          And...?
          • 2. Re: recursive method
            JosAH
            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
              807589
              SaintNicholas wrote:
              I need help trying to write a recursive method returning the number of occurrences of a specified character in the HTML String.
              Manage your threads better,

              http://forum.java.sun.com/thread.jspa?threadID=5308411&messageID=10311761#10311761

              If nothing else the above should've made it clear to you that you need to reveal more information about what you want.
              • 4. Re: recursive method
                807589
                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
                  807589
                  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
                    807589
                    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
                      796440
                      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.