From a79615eee96227505fba4f2818321a00eef1982d Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Fri, 31 Dec 2021 16:54:53 +0800 Subject: 5.5 --- 5/5.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/5/5.c b/5/5.c index 0c35198..f9156ef 100644 --- a/5/5.c +++ b/5/5.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -13,12 +14,16 @@ int main(int argc, char *argv[]) { char *s, *t; n = 5; - s = malloc(sizeof(char) * (n + 1)); + s = malloc(sizeof(char) * MAXLEN); t = "hello, world!"; - + mstrncpy(s, t, n); - printf("strncpy: %s\n", s); - + printf("mstrncpy: %s\n", s); + + t = "may the force be with you"; + mstrncat(s, t, 7); + printf("mstrncat: %s\n", s); + free(s); return 0; @@ -27,4 +32,9 @@ int main(int argc, char *argv[]) { void mstrncpy(char *s, char *t, int n) { for (; n > 0 && (*s++ = *t++) != 0; n--) ; +} + +void mstrncat(char *s, char *t, int n) { + for (s += strlen(s); n > 0 && (*s++ = *t++) != 0; n--) + ; } \ No newline at end of file -- cgit v1.2.3