summaryrefslogtreecommitdiff
path: root/ex5-5.c
blob: a6af2752109585690b136acad8eeefd1cb2c76a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}