This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
executable file
·49 lines (46 loc) · 1.64 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
44
45
46
47
48
49
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strnstr.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/10/26 20:10:12 by fbes #+# #+# */
/* Updated: 2022/02/08 21:51:40 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* Search for a string in a string
* @param[in] *str The string to search in
* @param[in] *sub The string to find
* @param[in] len The amount of characters to stop searching after
* @return A pointer to the string found, or NULL if not found
*/
char *ft_strnstr(const char *str, const char *sub, size_t len)
{
size_t found_len;
size_t to_find_len;
size_t i;
to_find_len = ft_strlen(sub);
if (to_find_len == 0)
return ((char *)str);
found_len = 0;
i = 0;
while (str[i] != '\0' && i < len)
{
if (str[i] == sub[found_len])
found_len++;
else if (found_len != to_find_len)
{
i = i - found_len;
found_len = 0;
}
else
break ;
i++;
}
if (found_len == to_find_len)
return ((char *)&str[i - found_len]);
return (NULL);
}