From bbfec50dc1ee109b6980605eb0d19e6a4641968a Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Wed, 29 Dec 2021 19:28:36 +0800 Subject: 5.3 --- 5/3.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 5/3.c (limited to '5/3.c') diff --git a/5/3.c b/5/3.c new file mode 100644 index 0000000..fe839a4 --- /dev/null +++ b/5/3.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#define MAXLEN 1000 + +void mstrcat(char *, char *); + +int main(int argc, char *argv[]) { + char *s, *t; + size_t n; + + s = malloc(MAXLEN); + t = malloc(MAXLEN); + n = sizeof(s); + + printf("first str: "); + getline(&s, &n, stdin); + + printf("second str: "); + getline(&t, &n, stdin); + + mstrcat(s, t); + printf("strcat: %s\n", s); + + return 0; +} + +void mstrcat(char *s, char *t) { + int i; + + for (i = 0; i < MAXLEN - 1 && *s++; i++) + ; + for (s -= 2; i < MAXLEN - 1 && (*s++ = *t++) != '\n'; i++) + ; + *s = 0; +} \ No newline at end of file -- cgit v1.2.3