-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_strnstr.c
63 lines (59 loc) · 2.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cjackows <cjackows@student.42wolfsburg. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/09 12:24:20 by cjackows #+# #+# */
/* Updated: 2022/06/19 15:55:10 by cjackows ### ########.fr */
/* */
/* ************************************************************************** */
#include "../inc/libft.h"
/**
* @brief Function locates the first occurrence of the null-terminated
* string 'needle' in the string 'haystack',
* where not more than 'len' characters are searched.
* Characters that appear after a \0 character are not searched.
* @param haystack Bigger string where it searches for 'needle'.
* @param needle Smaller string that is getting searched in 'haystack'.
* @param len Up to how many characters we are searching in 'haystack'.
* @return char* Returns pointer to position in 'haystack' if found.
* Returns pointer to 'haystack' if 'needle' is empty.
* Returns NULL if not found.
*/
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
size_t n_len;
char *hs;
i = 0;
hs = (char *) haystack;
n_len = ft_strlen(needle);
if (n_len == 0 || haystack == needle)
return (hs);
while (hs[i] && i < len)
{
j = 0;
while (hs[i + j] && needle[j] && hs[i + j] == needle[j]
&& i + j < len)
j++;
if (j == n_len)
return (hs + i);
i++;
}
return (0);
}
// int main(void)
// {
// const char lstr[] = "123423789";
// const char sstr[] = "237";
// char *ptr;
// ptr = ft_strnstr(lstr, sstr, 7);
// if(ptr == NULL)
// printf("We didn't find it\n");
// else
// printf("We found it on position - %ld\n", (ptr-lstr+1));
// return 0;
// }