-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
41 lines (38 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schetty <schetty@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/09 23:05:20 by schetty #+# #+# */
/* Updated: 2021/11/12 01:50:29 by schetty ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
size_t s1_len;
size_t start;
size_t end;
char *str;
if (!s1 || !set)
return (NULL);
s1_len = 0;
while (s1[s1_len])
s1_len++;
start = 0;
while (start < s1_len && ft_isset(s1[start], set))
start++;
end = s1_len;
while ((end - 1) > start && ft_isset(s1[end - 1], set))
end--;
s1_len = end - start;
str = malloc(sizeof(char) * (s1_len + 1));
if (!str)
return (NULL);
str[s1_len] = '\0';
while (s1_len--)
str[s1_len] = s1[start + s1_len];
return (str);
}