-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strstr.c
70 lines (61 loc) · 1.69 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gabdoush <gabdoush@42ABUDHABI.AE> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/21 20:47:14 by gabdoush #+# #+# */
/* Updated: 2021/11/01 13:55:19 by gabdoush ### ########.fr */
/* */
/* ************************************************************************** */
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
char *ft_strstr(const char *haystack, const char *needle)
{
if (*needle == '\0')
{
return (char *)haystack;
}
int i;
int j;
i = 0;
while (haystack[i] != '\0')
{
j = 0;
if (haystack[i] == needle[j])
{
while (haystack[i] == needle[j] && needle[j] != '\0')
{
i++;
j++;
}
}
else
{
i++;
}
if (needle[j] == '\0')
{
return (char *)(haystack + i - j);
}
}
return (0);
}
int main()
{
char main_string[] = "Ghaith AntAntiun Ghaith Abdoushj";
char string[] = "ji";
printf("%s\n", ft_strstr(main_string, string));
printf("%s\n", strstr(main_string, string));
if (strstr(main_string, string) == ft_strstr(main_string, string))
{
printf("True\n");
}
else
{
printf("False\n");
}
return EXIT_SUCCESS;
}