From 86b4fdf37b8096c9bb11e3f36752f121201ebcf9 Mon Sep 17 00:00:00 2001 From: Timo Schmidt Date: Fri, 24 Mar 2023 00:47:38 +0100 Subject: [PATCH] Fix ft_strncpy.c --- ex01/ft_strncpy.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/ex01/ft_strncpy.c b/ex01/ft_strncpy.c index 85a0e9f..8a542b8 100644 --- a/ex01/ft_strncpy.c +++ b/ex01/ft_strncpy.c @@ -6,19 +6,19 @@ /* By: tischmid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/19 10:25:26 by tischmid #+# #+# */ -/* Updated: 2023/03/19 18:05:09 by tischmid ### ########.fr */ +/* Updated: 2023/03/24 00:36:14 by tischmid ### ########.fr */ /* */ /* ************************************************************************** */ +#define BUFSIZE 10 + char *ft_strncpy(char *dest, char *src, unsigned int n) { - unsigned int i; - - i = -1; - while (++i < n && src[i] != '\0') - dest[i] = src[i]; - while (++i < n) - dest[i] = '\0'; + ++n; + while (--n && *src) + *dest++ = *src++; + while (n--) + *dest++ = 0; return (dest); } @@ -27,10 +27,15 @@ char *ft_strncpy(char *dest, char *src, unsigned int n) int main(void) { - char s1[10]; + char buf[BUFSIZE] = "||||||||||"; - ft_strncpy(s1, "Hello", 4); - printf("%s\n", s1); + for (int j = 0; j < 11; ++j) + { + printf("####### Size == %d #######\n", j); + ft_strncpy(buf, "Hei", j); + for (int i = 0; i < BUFSIZE; ++i) + printf((buf[i] >= 32 && buf[i] <= 126) ? "%c\n" : "0x%x\n", buf[i]); + } return (0); } */ ////