summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-12-31 16:46:02 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-12-31 16:46:02 +0800
commit47c9174a14fdc7bc5115d5a881a1656d2ef8dea9 (patch)
tree09427e3b30cccfad6a837807ae1326d4c5b32754
parentef44f44b21c88b1ae3e19348a5e5ef64d3ed2ed6 (diff)
downloadk&r-exercises-47c9174a14fdc7bc5115d5a881a1656d2ef8dea9.tar.gz
5.5
-rw-r--r--5/5.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/5/5.c b/5/5.c
new file mode 100644
index 0000000..0c35198
--- /dev/null
+++ b/5/5.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAXLEN 1000
+
+void mstrncpy(char *s, char *t, int n);
+void mstrncat(char *s, char *t, int n);
+void mstrncmp(char *s, char *t, int n);
+
+int main(int argc, char *argv[]) {
+ int n;
+ char *s, *t;
+
+ n = 5;
+ s = malloc(sizeof(char) * (n + 1));
+ t = "hello, world!";
+
+ mstrncpy(s, t, n);
+ printf("strncpy: %s\n", s);
+
+ free(s);
+
+ return 0;
+}
+
+void mstrncpy(char *s, char *t, int n) {
+ for (; n > 0 && (*s++ = *t++) != 0; n--)
+ ;
+} \ No newline at end of file