-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strnstr.c
117 lines (112 loc) · 3.28 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zakchouc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 09:48:12 by zakchouc #+# #+# */
/* Updated: 2024/12/02 22:34:16 by zakchouc ### ########.fr */
/* */
/* ************************************************************************** */
/**
* @file ft_strnstr.c
* @author Ziyad A. Dev ([email protected])
* @brief The strnstr() function locates the first occurrence of
* the null-terminated string little in the string big, where not more than
* len characters are searched through big.
* Characters that appear after a ‘\0’ character are not searched.
* Since the strnstr() function is a FreeBSD specific API, it should only
* be used when portability is not a concern.
* @param big
* @param little
* @param len
* @return If little is an empty string, big is returned;
* if little occurs nowhere in big, NULL is returned;
* otherwise a pointer to the first character of the first
* occurrence of little is returned.
* @version 0.1
* @date 2023-11-10
*
* @copyright Copyright (c) 2023
*
*/
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
char *bptr;
char *lptr;
if (!*little)
return ((char *)big);
while (len-- && *big)
{
if (*big == *little)
{
i = len;
bptr = (char *)big + 1;
lptr = (char *)little + 1;
while (i-- && *bptr && *lptr && *lptr == *bptr)
{
++lptr;
++bptr;
}
if (*lptr == '\0')
return ((char *)big);
}
++big;
}
return (NULL);
}
// char *ft_strnstr(const char *big, const char *little, size_t len)
// {
// size_t i;
// size_t j;
//
// if (!*little)
// return ((char *)big);
// i = 0;
// while (big[i])
// {
// j = 0;
// while (((i + j) < len) && (big[i + j] == little[j]))
// {
// if (!big[i + j] && !little[j])
// return ((char *)big + i);
// j++;
// }
// if (!little[j])
// return ((char *)big + i);
// i++;
// }
// return (NULL);
// }
//
// int main(int argc, char *argv[])
// {
// // int len;
// int i;
// // char sub[] = "ll";
// // char sub[10] = argv[1];
//
// i = 0;
// // len = 4;
// // len = atoi(argv[1]);
// if (argc < 1)
// return (0);
// (void)argv; //atoi(argv[1])
//
// while (i++ < 15)
// {
// // printf("\nsub : \"%s\"\tlen %d\tft strnstr : %s\n", sub, i,
// // ft1_strnstr("salamhellosalut", sub, i));
// // printf("sub : \"%s\"\tlen %d\tstrnstr ori : %s\n", sub, i,
// // strnstr("salamhellosalut", sub, i));
//
// printf("\nsub : \"%s\"\tlen %d\tft strnstr : %s\n", argv[1], i,
// ft_strnstr((NULL), argv[1], i));
// printf("sub : \"%s\"\tlen %d\tstrnstr ori : %s\n", argv[1], i,
// strnstr((NULL), argv[1], i));
// }
// return (0);
// }