-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
41 lines (37 loc) · 1.44 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: lpicoli- < lpicoli-@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 18:11:50 by lpicoli- #+# #+# */
/* Updated: 2022/11/17 22:58:21 by lpicoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
char *trim_str;
int start;
int end;
if (!s1 || !set)
return (NULL);
start = 0;
end = ft_strlen(s1);
while (s1[start] && ft_strchr(set, s1[start]))
start++;
while (s1[end - 1] && ft_strrchr(set, s1[end - 1]) && end > start)
end--;
trim_str = (char *)malloc((end - start + 1) * sizeof(char));
if (trim_str)
ft_strlcpy(trim_str, &s1[start], end - start + 1);
else
return (0);
return (trim_str);
}
/*int main()
{
char *trim_str = ft_strtrim("abqbc", "abc");
puts(trim_str);
}*/