Count string length by recursion!!

Joined
Nov 8, 2006
Messages
1
Reaction score
0
:rockon: Hi all,

As an exercise, I am trying to come up with a function to count the
length of a character string using recursion. I came up with this:

As a recursion pattern:

int count_len( const char s[ ] ){
if ( s == ‘\0’) {
return 0; // may return other things
} else {
return count_len ( input-1 ); // means a smaller input will be put in the
}

It came out with error :| after I formed the counting length of character program.

Therefore, how could I form another program which applies to count the length of character??

Suppose there is NO global variables in the function. But it is allowed to write another function containing other the formal parameters for length count aid.
 
Joined
Nov 13, 2006
Messages
2
Reaction score
0
here is my solution:
int count_len( const char * const s ){
if( *s == '\0' )
return 0;
else
return 1 + count_len( s+1 );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top