-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
67 lines (59 loc) · 1.79 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
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: xle-boul <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/13 11:04:36 by xle-boul #+# #+# */
/* Updated: 2021/10/24 11:00:44 by xle-boul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* removes the give set of characters from the beginning and
the end of the string. It stops as soon as it encounters
any other character along the way. */
static int ft_char_in_set(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t i;
size_t start;
size_t end;
start = 0;
while (s1[start] != '\0' && ft_char_in_set(s1[start], set))
start++;
end = ft_strlen(s1);
while (end > start && ft_char_in_set(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(*s1) * (end - start + 1));
if (!str)
return (NULL);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = '\0';
return (str);
}
/*
int main()
{
char s[] = "lorem ipsum dolor sit amet";
char set[] = "te";
char *str;
str = ft_strtrim(s, set);
printf("%s\n", str);
return (0);
}
*/