That does not quite work. consider the case where num == 0 to begin with.
How many digits does the number 0 have? Your solution returns 0; tifa's
returns 1.
int num_digits_helper( int num ) {
return num ? 1 + num_digits_helper( num / 10 ) : 0;
}
int num_digits( int num ) {
return num ? num_digits_helper( num ) : 1;
}
is better, as long as the helper function is not accessible to the user.