-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strnstr.c
43 lines (39 loc) · 1.44 KB
/
ft_strnstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gcosta-d <gcosta-d@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/20 16:36:12 by gcosta-d #+# #+# */
/* Updated: 2021/09/14 10:18:14 by gcosta-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* Strnstr finds if there is a little string in a big string.
* The comparissons must be done until find the little string,
* or until little string fits into the last bytes of big string.
*/
char *ft_strnstr(const char *s1, const char *s2, size_t n)
{
size_t i;
size_t j;
size_t s2_len;
i = 0;
s2_len = ft_strlen(s2);
if (!s2_len)
return ((char *)s1);
if (n != 0)
{
while (s1[i] && i <= n - s2_len)
{
j = 0;
while (s2[j] && s2[j] == s1[i + j])
j++;
if (j == s2_len)
return ((char *)&s1[i]);
i++;
}
}
return (NULL);
}