-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
56 lines (51 loc) · 1.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/29 14:05:24 by abdsalah #+# #+# */
/* Updated: 2024/09/01 14:09:53 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strncpy(char *dest, const char *src, size_t n)
{
size_t i;
i = 0;
while (i < n && src[i] != '\0')
{
dest[i] = src[i];
i++;
}
while (i < n)
dest[i++] = '\0';
return (dest);
}
char *ft_strtrim(const char *s1, const char *set)
{
const char *start;
const char *end;
char *out;
size_t len;
if (s1 == NULL)
return (NULL);
if (set == NULL || *set == '\0')
return (ft_strdup(s1));
start = s1;
while (*start && ft_strchr(set, *start))
start++;
if (*start == '\0')
return (ft_strdup(""));
end = s1 + ft_strlen(s1) - 1;
while (end > start && ft_strchr(set, *end))
end--;
len = (end - start) + 1;
out = malloc(len + 1);
if (out == NULL)
return (NULL);
ft_strncpy(out, start, len);
out[len] = '\0';
return (out);
}