From 2cfbe649504577c72c6e5129ebacca441b3b6cc3 Mon Sep 17 00:00:00 2001 From: Teodor Dutu Date: Thu, 17 Oct 2024 15:16:22 +0300 Subject: [PATCH] chapters/software-stack: Simplify `os_strcat()` implementation. 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 --- .../common-functions/solution/src/os_string.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/chapters/software-stack/libc/drills/tasks/common-functions/solution/src/os_string.c b/chapters/software-stack/libc/drills/tasks/common-functions/solution/src/os_string.c index 1862f4a258..c6ab6263e0 100644 --- a/chapters/software-stack/libc/drills/tasks/common-functions/solution/src/os_string.c +++ b/chapters/software-stack/libc/drills/tasks/common-functions/solution/src/os_string.c @@ -23,7 +23,7 @@ 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; @@ -31,8 +31,22 @@ char *os_strcat(char *dest, const char *src) 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; }