-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strnstr.c
More file actions
43 lines (39 loc) · 1.62 KB
/
ft_strnstr.c
File metadata and controls
43 lines (39 loc) · 1.62 KB
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: farodrig <farodrig@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/12/04 12:10:46 by farodrig #+# #+# */
/* Updated: 2021/02/28 20:46:25 by farodrig ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Locates the first occurrence of the null-terminated string str2 in the
** string str1, where not more than len characters are searched.
** Characters that appear after a `\0' character are not searched. If str2 is
** an empty string, str1 is returned; if str2 occurs nowhere in str1,
** NULL is returned; otherwise a pointer to the first character of the first
** occurrence of str2 is returned.
*/
char *ft_strnstr(const char *str1, const char *str2, size_t len)
{
size_t str2_len;
if (*str2 == 0)
{
return ((char *)str1);
}
str2_len = ft_strlen(str2);
while (*str1 != 0 && len >= str2_len)
{
if (*str1 == *str2 && ft_memcmp(str1, str2, str2_len) == 0)
{
return ((char *)str1);
}
str1++;
len--;
}
return (0);
}