-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
51 lines (46 loc) · 1.49 KB
/
ft_strtrim.c
File metadata and controls
51 lines (46 loc) · 1.49 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aycami <aycami@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 14:56:14 by aycami #+# #+# */
/* Updated: 2024/10/07 14:56:22 by aycami ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_control(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i++] == c)
return (1);
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t start;
size_t end;
size_t i;
if (!s1 || !set)
return (NULL);
start = 0;
end = ft_strlen(s1);
while (s1[start] && ft_control(s1[start], set))
start++;
while (end > start && ft_control(s1[end - 1], set))
end--;
str = (char *)malloc(sizeof(char) * (end - start) + 1);
if (!str)
return (NULL);
i = 0;
while (start < end)
str[i++] = s1[start++];
str[i] = '\0';
return (str);
}