Thread: Recursion Practice

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Recursion Practice

    Hi,

    Im just getting round the concept of recursion and have been doing a few set practice questions.

    One was to count the characters in a word (recursively)

    The other was to count the number of words in a sentence (also recursively) which is here:

    code:
    Code:
    int wordCount(char *sen){
        if(*sen == '\0')
            return 0;
        
        else{
        while((*sen != ' ') && (*sen != '\0'))
            ++sen;
        
        if(*sen == '\0')
            return 1;
        else
            return (1 + wordCount(++sen));
        }
    }
    Now what i have done works, however what i wanted to ask is if this could be done more elegantly. I mean I was supposed to use recursive principles (which i have) however using the loop to find the next word means that its probably not the best way to achieve the result im looking for.

    However maybe im wrong and this is the way to recursively do it.

    Thanks for any comments!

    -Alex

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >One was to count the characters in a word (recursively)
    Um, why? Recursion doesn't buy you anything and it's very expensive and limited when compared to the trivial non-recursive loop.

    >The other was to count the number of words in a sentence (also recursively)
    Same thing. Why? I don't see how either of your practice questions give you any understanding of recursion beyond "a function calls itself".
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by Prelude View Post
    >One was to count the characters in a word (recursively)
    Um, why? Recursion doesn't buy you anything and it's very expensive and limited when compared to the trivial non-recursive loop.

    >The other was to count the number of words in a sentence (also recursively)
    Same thing. Why? I don't see how either of your practice questions give you any understanding of recursion beyond "a function calls itself".
    I KNOW...

    its just practice, breaking down a problem into smaller problems and using those to answer to larger problem.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >its just practice
    Why would you want to practice bad habits? Recursion is unsuited to these problems. If you want to practice, find a problem that's well suited to recursion. Solve that problem with recursion, with simulated recursion, and without recursion. That's practice. But since you want to be stubborn, I'll answer your questions seriously.

    >what i wanted to ask is if this could be done more elegantly.
    Yes, don't use recursion. However, if you have to use recursion, keep in mind that your code is broken. You don't account for runs of whitespace. As a more elegant solution, I would submit this:
    Code:
    #include <cctype>
    
    int wordCount ( const char *line )
    {
      // Skip leading whitespace
      while ( *line != '\0' && std::isspace ( (unsigned char)*line ) )
        ++line;
    
      if ( *line != '\0' ) {
        // Jump over the next word
        while ( *line != '\0' && !std::isspace ( (unsigned char)*line ) )
          ++line;
    
        return 1 + wordCount ( line );
      }
    
      return 0;
    }
    >using the loop to find the next word means that its probably not
    >the best way to achieve the result im looking for.
    It depends on what you're looking for. I'd say that it simplifies the problem greatly on top of decreasing the recursive depth.
    My best code is written with the delete key.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by appleGuy View Post
    I KNOW...

    its just practice, breaking down a problem into smaller problems and using those to answer to larger problem.
    Don't let Prelude discourage you. Answering the question "How would I do this task recursively?" is a healthy endeavor no matter what language you're using. Just keep in mind that C++ is inherently an imperative language where recursion often is NOT the best way to solve a problem.

    It doesn't hurt to practice it. Just don't let it wow you into thinking you should be using it everywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM