-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
42 lines (39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chanheki <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/06 19:23:11 by chanheki #+# #+# */
/* Updated: 2022/07/25 22:51:50 by chanheki ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
char *result;
if (!s1)
return (NULL);
if (!set)
return (ft_strdup(s1));
start = 0;
end = ft_strlen(s1);
while (s1[start] && ft_strchr(set, s1[start]))
start++;
while (s1[end - 1] && ft_strchr(set, s1[end - 1]))
{
if (end - 1 < 1)
break ;
end--;
}
if (start > end)
return (ft_strdup(""));
result = (char *)malloc(sizeof(char) * (end - start + 1));
if (!result)
return (NULL);
ft_strlcpy(result, s1 + start, end - start + 1);
return (result);
}