diff --git a/Makefile b/Makefile index 2ce252a..1c222cc 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/16 15:22:08 by kcolin ### ########.fr # +# Updated: 2024/10/16 15:38:09 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -30,7 +30,8 @@ SOURCES = ft_isalpha.c \ ft_strrchr.c \ ft_strncmp.c \ ft_memchr.c \ - ft_memcmp.c + ft_memcmp.c \ + ft_strnstr.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..e2ebdcc --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/16 15:38:24 by kcolin #+# #+# */ +/* Updated: 2024/10/16 15:58:22 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t i; + size_t j; + int found; + + if (*little == '\0') + return ((char *)big); + i = 0; + while (big[i] != '\0' && i < len) + { + j = 0; + found = 1; + while (little[j] != '\0' && i + j < len) + { + if (little[j] != big[i + j]) + { + found = 0; + break ; + } + j++; + } + if (found == 1 && little[j] == '\0') + return ((char *)big + i); + i++; + } + return (NULL); +} + +/* +#include +#include + +int main(int argc, char **argv) +{ + size_t cmp_len; + + cmp_len = 40; + if (argc > 2) + { + printf("mine: %s\nlib: %s\n", + ft_strnstr(argv[1], argv[2], cmp_len), + strnstr(argv[1], argv[2], cmp_len)); + } + return (0); +} +*/ diff --git a/libft.h b/libft.h index ea82cf6..c7277dc 100644 --- a/libft.h +++ b/libft.h @@ -40,4 +40,6 @@ int ft_strncmp(const char *s1, const char *s2, size_t n); void *ft_memchr(const void *s, int c, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n); +char *ft_strnstr(const char *big, const char *little, size_t len); + #endif