-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
38 lines (35 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gpuscedd <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 18:32:48 by gpuscedd #+# #+# */
/* Updated: 2024/01/19 21:40:39 by gpuscedd ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
size_t new_len;
char *trimmed;
if (!s1)
return (NULL);
if (!*s1)
return (ft_strdup(""));
start = 0;
end = ft_strlen(s1) - 1;
while (start <= end && ft_strchr(set, s1[start]))
start++;
while (end >= start && ft_strchr(set, s1[end]))
end--;
new_len = end - start + 2;
trimmed = malloc((new_len) * sizeof(char));
if (!trimmed)
return (NULL);
ft_strlcpy(trimmed, s1 + start, new_len);
return (trimmed);
}