I'm inventing interview questions that require analysis of C++ code that does something simple with pointers and recursion. I tried to write strcat()
in recursive manner:
size_t mystrlen( const char* str )
{
if( *str == 0 ) {
return 0;
}
return 1 + mystrlen( str + 1 );
}
void mystrcpy( char* to, const char* from )
{
if( ( *to = *from ) == 0 ) {
return;
}
mystrcpy( to + 1, from + 1 );
}
void mystrcat( char* to, const char* from )
{
mystrcpy( to + mystrlen( to ), from );
}
What I don't like here is that I have three functions and my strcat()
is not very recursive - it just calls two other functions once. Is there a way to rewrite that somehow to reduce the number of functions increasing use of recursion but without sacrificing much of code brevity?