Monday, September 26, 2011

Tweet-sized code #1

Author: Sudarshan Murthy


NODE* c=first;while(c?strcmp(c->val,s):0)c=c->next;return c;

1. What does this C fragment do?

2. Ignoring the lengths of variable names and type names, can this segment be any shorter? How, or, why not?

3. As shown, the fragment has a variable declaration, a loop with one statement in its body, and a return statement outside the loop. Is it possible to rewrite the segment to contain just a loop? It is OK if the variable declaration and the return statement are made part of the loop, but the loop body should be empty. It is also OK if the code gets slightly (but not significantly) longer. If it can be done, how? If it cannot be done, why not?

My take in another week or so. I rate these questions Easy.