ft_strnstr: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-16 15:59:48 +02:00
parent 1c3df31518
commit 0a1e2f2b37
3 changed files with 66 additions and 2 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

61
ft_strnstr.c Normal file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdio.h>
#include <bsd/string.h>
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);
}
*/

View file

@ -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