From 87b3901e4ed2ae5cff29e8ccfc6b7b78aa65260e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 30 Oct 2023 10:56:10 +0100 Subject: [PATCH] strcpy --- src/miniio.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/miniio.c b/src/miniio.c index 0dadd8d..3088db9 100644 --- a/src/miniio.c +++ b/src/miniio.c @@ -9,12 +9,34 @@ int my_strlen(const char *s) { return len; } +void my_strcpy(char *dest, const char *src) { + register int len = 0; + while (src[len]) { + len++; + } + len++; // we have a NULL byte + for (int i = 0; i < len; i++) { + dest[i] = src[i]; + } +} + int main() { char word[] = "hallo\0"; // not NULL terminated + char my_buf[6]; + char std_buf[6]; + printf("strlen\n"); printf("word is %d long\n", my_strlen(word)); printf("word is actually %lu long\n", strlen(word)); - printf("success: %b", my_strlen(word) == strlen(word)); + printf("success: %b\n", my_strlen(word) == strlen(word)); + + printf("\nstrcpy\n"); + my_strcpy(my_buf, word); + strcpy(std_buf, word); + printf("word: \"%s\"\n", word); + printf("my_buf: \"%s\"\n", my_buf); + printf("std_buf: \"%s\"\n", std_buf); + printf("success: %b\n", 0 == strcmp(my_buf, std_buf)); return 0; }