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:
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.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)); } }
However maybe im wrong and this is the way to recursively do it.
Thanks for any comments!
-Alex