-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
53 lines (48 loc) · 1.51 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
50
51
52
53
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abkssiba <abkssiba@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/21 12:24:29 by abkssiba #+# #+# */
/* Updated: 2021/05/24 16:06:44 by abkssiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_isset(char const *set, char c)
{
int i;
int len;
len = ft_strlen(set);
i = 0;
while (i < len)
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
int start;
int end;
if (!s1)
return (NULL);
start = 0;
end = ft_strlen(s1) - 1;
while (ft_isset(set, s1[start]))
start++;
while (ft_isset(set, s1[end]) && end > 0)
end--;
if (end < start)
return (ft_strdup(""));
str = (char *)malloc(end - start + 2);
if (!str)
return (NULL);
ft_memmove(str, &s1[start], end - start + 1);
str[end - start + 1] = '\0';
return (str);
}