-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
54 lines (49 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yaajagro <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 17:44:28 by yaajagro #+# #+# */
/* Updated: 2024/11/05 21:18:33 by yaajagro ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_spliter(char c, const char *str)
{
int i;
i = 0;
while (str[i])
{
if (c == str[i])
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t index;
char *trimed;
size_t len;
if ((!s1 && !set) || !s1)
return (NULL);
if (s1 && !set)
return (ft_strdup(s1));
start = 0;
index = 0;
len = ft_strlen(s1);
while (s1[start] && ft_spliter(s1[start], set))
start++;
while (len > start && ft_spliter(s1[len - 1], set))
len--;
trimed = malloc(len - start + 1);
if (!trimed)
return (NULL);
while (start < len)
trimed[index++] = s1[start++];
trimed[index] = '\0';
return (trimed);
}