From cabe1a14b31edce1a247c367f8e1d7265abec34e Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Tue, 7 Feb 2023 22:20:47 +0000 Subject: Chapter 5 --- ex5-5.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 ex5-5.c (limited to 'ex5-5.c') 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 + +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 -- cgit v1.2.3-ZIG