From a8a3cbe224c60f1602c095f6bb15e320f9f4c498 Mon Sep 17 00:00:00 2001 From: Flinner Yuu Date: Mon, 18 Jul 2022 08:15:40 +0300 Subject: [PATCH] feat: 5-3 --- chapter-5/5-3.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 chapter-5/5-3.c diff --git a/chapter-5/5-3.c b/chapter-5/5-3.c new file mode 100644 index 0000000..a1d055c --- /dev/null +++ b/chapter-5/5-3.c @@ -0,0 +1,35 @@ +/* Exercise 5-3. Write a pointer version of the function strcat that we showed + * in Chapter 2: strcat(s,t) copies the string t to the end of s. */ + +#include +#include + +// version 1 +void my_strcat1(char *s, char *t) { + while (*s) + s++; // go to end of s + + while ((*s = *t) != '\0') { + s++; + t++; + } +} + +// version 2 +void my_strcat2(char *s, char *t) { + while (*s) + s++; // go to end of s + while ((*s++ = *t++)) + ; // then copy +} + +int main() { + char s[50] = "Hello, "; + char t[] = "world!"; + + my_strcat2(s, t); + + printf("%s\n", s); + + return 0; +}