summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-31 16:54:53 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-31 16:54:53 +0800
commita79615eee96227505fba4f2818321a00eef1982d (patch)
tree3b5deb10be6e427cc810fed7ab32c95550508ada
parente0384bb43035832ad6d091f5d7dda1c8d1014a9a (diff)
downloadk&r-exercises-a79615eee96227505fba4f2818321a00eef1982d.tar.gz
5.5
-rw-r--r--5/5.c18
1 files 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 <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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