1. ## 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.

-Alex

2. >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".

3. Originally Posted by Prelude
>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. >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 )
{
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.

5. Originally Posted by appleGuy
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.