summaryrefslogtreecommitdiff
path: root/ex5-5.c
diff options
context:
space:
mode:
Diffstat (limited to 'ex5-5.c')
-rw-r--r--ex5-5.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/ex5-5.c b/ex5-5.c
new file mode 100644
index 0000000..a6af275
--- /dev/null
+++ b/ex5-5.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+char* z_strncpy(char *dest, char *src, size_t n) {
+ char* max = dest + n;
+ for (; *src != '\0' && dest < max; dest++, src++) {
+ *dest = *src;
+ }
+ for (; dest < max; dest++) {
+ *dest = '\0';
+ }
+ return dest;
+}
+
+int main(void) {
+ char buf[100] = "";
+ z_strncpy(buf, "Hello, World", 5);
+ printf("Hello = %s\n", buf);
+ z_strncpy(buf, "Hello, World", 20);
+ printf("Hello, World = [%s]\n", buf);
+} \ No newline at end of file