From aad909fe230a89f274a02eb536eb5c3f28fea905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 16 Oct 2024 14:21:59 +0200 Subject: [PATCH] ft_memchr: initial implementation --- Makefile | 5 +++-- ft_memchr.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 2 ++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 ft_memchr.c diff --git a/Makefile b/Makefile index 4ff2061..c99db96 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/16 13:22:00 by kcolin ### ########.fr # +# Updated: 2024/10/16 13:50:09 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -28,7 +28,8 @@ SOURCES = ft_isalpha.c \ ft_tolower.c \ ft_strchr.c \ ft_strrchr.c \ - ft_strncmp.c + ft_strncmp.c \ + ft_memchr.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_memchr.c b/ft_memchr.c new file mode 100644 index 0000000..851fdd4 --- /dev/null +++ b/ft_memchr.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/16 14:01:28 by kcolin #+# #+# */ +/* Updated: 2024/10/16 14:05:38 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, t_size n) +{ + t_size i; + + i = 0; + while (i < n) + { + if (((const unsigned char *)s)[i] == (unsigned char)c) + return ((void *)s + i); + i++; + } + return (0); +} + +/* +#include +#include + +int main(int argc, char **argv) +{ + char *result; + t_size cmp_len; + + cmp_len = 5; + if (argc > 2) + { + result = ft_memchr(argv[1], argv[2][0], cmp_len); + if (result == 0) + printf("(null)\n"); + else + printf("%s\n", result); + result = memchr(argv[1], argv[2][0], cmp_len); + if (result == 0) + printf("(null)\n"); + else + printf("%s\n", result); + } + return (0); +} +*/ diff --git a/libft.h b/libft.h index 2eb7506..0619fd4 100644 --- a/libft.h +++ b/libft.h @@ -37,4 +37,6 @@ char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); int ft_strncmp(const char *s1, const char *s2, t_size n); +void *ft_memchr(const void *s, int c, t_size n); + #endif