-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strstr.c
49 lines (45 loc) · 1.6 KB
/
ft_strstr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vinguyen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/01 00:02:07 by vinguyen #+# #+# */
/* Updated: 2019/10/01 00:02:09 by vinguyen ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Locates the first occurence of the null-terminated string needle in the
** haystack. const char *haystack, const char *needle)
** Return:
** needle == '\0' -> haystack
** needle not in haystack -> NULL
** otherwise, pointer to first char of the first occurence of needle
*/
#include "libft.h"
char *ft_strstr(const char *haystack, const char *needle)
{
const char *c1;
const char *c2;
int traverse;
c1 = haystack;
c2 = needle;
traverse = 0;
if (*needle == '\0')
return ((char*)haystack);
while (*c1 != '\0' && *c2 != '\0')
{
if (*c1 == *c2)
{
while (*(c1 + traverse) == *(c2 + traverse))
{
if (*(c2 + traverse + 1) == '\0')
return ((char*)c1);
traverse++;
}
}
c1++;
}
return (NULL);
}