ft_strnstr

This commit is contained in:
Tanguy MAZE 2018-04-05 11:48:44 +02:00
parent aa17bb3c11
commit e50dcca5e8
3 changed files with 37 additions and 3 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <marvin@42.fr> +#+ +:+ +#+ # # By: tmaze <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2018/04/04 10:51:59 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_strchr.c \
ft_strrchr.c \ ft_strrchr.c \
ft_strstr.c \ ft_strstr.c \
ft_strnstr.c \
ft_isalpha.c \ ft_isalpha.c \
ft_isdigit.c \ ft_isdigit.c \
ft_isalnum.c \ ft_isalnum.c \

33
ft_strnstr.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */ /* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/04 10:40:01 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_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c); char *ft_strrchr(const char *s, int c);
char *ft_strstr(const char *haystack, const char *needle); 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_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2); int ft_strncmp(const char *s1, const char *s2);
int ft_atoi(const char *nptr); int ft_atoi(const char *nptr);