-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
50 lines (46 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aabda <aabda@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 16:50:34 by aabda #+# #+# */
/* Updated: 2022/07/18 18:03:50 by aabda ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check_ismatch(char c, char const *set)
{
while (*set)
{
if (c == *set)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
int start;
int end;
if (!s1 || !set)
return (NULL);
start = 0;
end = ft_strlen(s1) - 1;
while (check_ismatch(s1[start], set) == 1)
start++;
if (start - 1 == end)
{
str = malloc(sizeof(char));
if (!str)
return (NULL);
str[0] = '\0';
return (str);
}
while (check_ismatch(s1[end], set) == 1)
end--;
str = ft_substr(s1, start, (end - start) + 1);
return (str);
}