-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
55 lines (50 loc) · 1.53 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
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: f██████ <f██████@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 16:09:35 by f██████ #+# #+# */
/* Updated: 2021/11/22 10:31:09 by f██████ ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char is_set(char c, char *s)
{
int i;
i = 0;
while (s[i])
{
if (c == s[i])
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
int i;
int j;
int s1_len;
int s2_len;
char *s2;
if (!s1)
return (0);
s1_len = ft_strlen(s1);
i = 0;
j = s1_len;
while (is_set(s1[i], (char *)set))
i++;
while (is_set(s1[--j], (char *)set))
if (j == 0)
break ;
s2_len = (s1_len - i) - (s1_len - j) + 2;
if (s2_len < 1)
s2_len = 1;
s2 = ft_calloc(s2_len, sizeof(char));
if (!s2)
return (0);
ft_strlcpy(s2, (char *)&s1[i], s2_len);
return (s2);
}