-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
49 lines (45 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
47
48
49
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tauer <tauer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 15:44:50 by tauer #+# #+# */
/* Updated: 2023/11/06 15:44:50 by tauer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char c, char const *set)
{
while (*set)
{
if (*set == c)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t start;
size_t end;
size_t i;
if (!s1)
return (NULL);
start = 0;
end = ft_strlen(s1);
i = 0;
while (s1[start] && is_set(s1[start], set) == 1)
start++;
while (end > start && is_set(s1[end - 1], set) == 1)
end--;
str = (char *)malloc(sizeof(char) * (end - start + 1));
if (!str)
return (NULL);
while (start < end)
str[i++] = s1[start++];
str[i] = '\0';
return (str);
}