S11 Answer


/* Puzzle S11 -- Are two strings equal? */

int areEqual( char const *left, char const *right )
{
  while ( *left && *right && *left == *right ) {left++; right++;}
  return *left==*right;
}


Comments:

The function is checking if two strings are equal, so it initializes two pointers left and right to point to the first character in each one.

Two Unequal Strings

The while-statement

while ( *left && *right && *left == *right ) {left++; right++;}

does this:

First, it checks if there is a character at the end of each pointer. (Recall that any non-NULL counts as true, so *left will count as true if it points to a character.) Then it checks that the two characters are equal. If so, both pointers are incremented, and the loop repeats.

The loop exits when either BOTH pointers point at NULL, or the pointers each point at something different. The expression

return *left==*right;

returns the correct value.