Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In a single function you could write:</p> <pre><code>// pre: to is a string that is zero-initiallized (all chars are '\0') void mystrcat( char* to, const char* from ) { if ( *to ) mystrcat( to+1, from ); else { if (*to = *from) mystrcat( to+1, from+1 ); } } </code></pre> <p>Note that it is intentionally ugly, it serves to purposes in two branches, it advances the <code>to</code> pointer in the first branch and it copies values in the other. The if condition is intentionally ugly also as most usually you will flag a single = within a condition as a typo, but insist in that it is up, running and tested so that they must work the meaning out themselves.</p> <p>One other thing you might consider is adding a return statement like:</p> <pre><code>char* mystrcat2( char* to, const char* from ) { if ( *to ) return mystrcat2( to+1, from ); else { if ( *to = *from ) return mystrcat2( to+1, from+1 ); else return to; } } </code></pre> <p>Now the interviewed has the second task of interpreting the result of the function when called on two strings, and you can discuss how the efficiency of this <code>strcat</code> version compares to the original both in terms of single string concatenations as well as multiple concatenations (take a look at <a href="http://www.joelonsoftware.com/articles/fog0000000319.html" rel="nofollow noreferrer">this article</a> by Joel Spolsky that among other things talks about the Scheimel paint algorithm). You can ask for the implementation of this function in terms of both the <code>strcat</code> and <code>mystrcat</code>:</p> <pre><code>// concatenates a set of strings into a buffer // // pre: to has memory enough to hold all the contents // from[i] is a valid C string for all i smaller than n void mymultiplecat( char* to, const char* from[], int n ) { for ( int i = 0; i &lt; n; ++i ) { to = mystrcat( to, from[i] ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload