-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
46 lines (42 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yeonhkim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/21 18:38:52 by yeonhkim #+# #+# */
/* Updated: 2022/07/23 10:45:40 by yeonhkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in_set(const char c, const char *set)
{
while (*set)
{
if (c == *set++)
return (1);
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *dst;
char *start;
char *end;
if (!set)
return (ft_strdup(s1));
start = (char *)s1;
end = (char *)(s1 + ft_strlen(s1) - 1);
while (is_in_set(*start, set))
start++;
if (!*start)
return (ft_strdup(""));
while (is_in_set(*end, set))
end--;
dst = malloc(sizeof(char) * (end - start + 1) + 1);
if (!dst)
return (NULL);
ft_strlcpy(dst, start, (end - start + 1) + 1);
return (dst);
}