From e50dcca5e8adc126ca81c5d2a494d089879eb68c Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Thu, 5 Apr 2018 11:48:44 +0200 Subject: [PATCH] ft_strnstr --- Makefile | 3 ++- ft_strnstr.c | 33 +++++++++++++++++++++++++++++++++ libft.h | 4 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 ft_strnstr.c diff --git a/Makefile b/Makefile index 4e68d96..b0ffb20 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2018/04/04 10:51:59 by tmaze #+# #+# # -# Updated: 2018/04/05 11:06:04 by tmaze ### ########.fr # +# Updated: 2018/04/05 11:40:07 by tmaze ### ########.fr # # # #******************************************************************************# @@ -26,6 +26,7 @@ SRCS = \ ft_strchr.c \ ft_strrchr.c \ ft_strstr.c \ + ft_strnstr.c \ ft_isalpha.c \ ft_isdigit.c \ ft_isalnum.c \ diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..d88f800 --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 11:35:03 by tmaze #+# #+# */ +/* Updated: 2018/04/05 11:47:18 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + int i; + int j; + char *tmp; + + i = -1; + tmp = (char*)haystack; + while ((++i == 0 || tmp[i - 1]) && i < (int)len) + if (tmp[i] == needle[0] && (j = 1)) + { + while (tmp[i + j] == needle[j] && needle[j] && tmp[i + j]\ + && (i + j) < (int)len) + j++; + if (needle[j] == '\0') + return (&tmp[i]); + } + return (NULL); +} diff --git a/libft.h b/libft.h index e61678c..f403b25 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/04 10:40:01 by tmaze #+# #+# */ -/* Updated: 2018/04/04 18:08:25 by tmaze ### ########.fr */ +/* Updated: 2018/04/05 11:39:51 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); char *ft_strstr(const char *haystack, const char *needle); -char *ft_strnstr(const char *haystack, const char *needle, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); int ft_strcmp(const char *s1, const char *s2); int ft_strncmp(const char *s1, const char *s2); int ft_atoi(const char *nptr);