Skip to content

Commit

Permalink
chapters/software-stack: Simplify os_strcat() implementation.
Browse files Browse the repository at this point in the history
The solution provided to `os_strcat()` was too esotheric and not suited
to be used by students wanting go learn how to implement `strcat()`.
The current solution is a bit more verbose, but still includes the
previous approac as a nice-to-know gimmick.

Signed-off-by: Teodor Dutu <[email protected]>
  • Loading branch information
teodutu committed Oct 17, 2024
1 parent 399fd0d commit 2cfbe64
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@ char *os_strcpy(char *dest, const char *src)
return dest;
}

/* TODO 12: Implement strcat(). */
/* TODO 25: Implement os_strcat(). */
char *os_strcat(char *dest, const char *src)
{
char *rdest = dest;

while (*dest)
dest++;

while (*src) {
*dest = *src;
dest++;
src++;
}

/*
* A more difficult to understand, but shorter and smarter implementation is below.
* The ++ operator is evaluated last, after checking the condition of the loop, so this condition can be
* seen as simply *dest = *src followed by dest++ and src++ in the loop body, which is equivalent to the loop
* above.
*/
/*
while (*dest++ = *src++)
;
*/

return rdest;
}

0 comments on commit 2cfbe64

Please sign in to comment.