/ src / ft_strnstr.c
ft_strnstr.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   ft_strnstr.c                                       :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: abonnard <abonnard@student.42nice.fr>      +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2024/11/04 17:37:52 by abonnard          #+#    #+#             */
 9  /*   Updated: 2024/11/05 16:49:50 by abonnard         ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include <stdio.h>
14  
15  void	*ft_strnstr(const char *haystack, const char *needle, size_t len)
16  {
17  	size_t	i;
18  	size_t	j;
19  
20  	i = 0;
21  	if (!*needle)
22  		return ((void *)haystack);
23  	while (haystack[i] && i < len)
24  	{
25  		j = 0;
26  		while (needle[j] && haystack[i + j] == needle[j] && i + j < len)
27  			j++;
28  		if (!needle[j])
29  			return ((void *)&haystack[i]);
30  		i++;
31  	}
32  	return (0);
33  }