-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
47 lines (43 loc) · 1.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ctokuyos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 01:04:47 by tokuyoshi #+# #+# */
/* Updated: 2024/11/06 16:34:23 by ctokuyos ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_in_set(char c, const char *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
size_t len;
char *trimmed;
if (!s1 || !set)
return (NULL);
start = 0;
while (s1[start] && is_in_set(s1[start], set))
start++;
end = ft_strlen(s1);
while (end > start && is_in_set(s1[end - 1], set))
end--;
len = end - start;
trimmed = (char *)malloc((len + 1) * sizeof(char));
if (!trimmed)
return (NULL);
ft_strlcpy(trimmed, &s1[start], len + 1);
return (trimmed);
}