-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
48 lines (43 loc) · 1.43 KB
/
ft_strtrim.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apimikov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:24:20 by apimikov #+# #+# */
/* Updated: 2023/11/06 16:05:09 by apimikov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int in_set(char c, char const *set);
char *ft_strtrim(char const *s1, char const *set)
{
size_t len;
size_t i1;
size_t i2;
char *pnt;
if (!s1 || !set)
return (NULL);
i1 = 0;
len = ft_strlen(s1);
i2 = len - 1;
while (s1[i1] && in_set(s1[i1], set))
i1++;
if (i1 == len)
return (ft_strdup(""));
while (in_set(s1[i2], set) && i2)
i2--;
pnt = ft_substr(s1, (unsigned int)i1, (size_t)(i2 - i1 + 1));
return (pnt);
}
int in_set(char c, char const *set)
{
while (*set)
{
if (*set == c)
return (1);
set++;
}
return (0);
}