-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
66 lines (59 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mattig <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/30 13:48:16 by mattig #+# #+# */
/* Updated: 2021/11/05 17:33:47 by mattig ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int ft_get_start(char const *s1, char const *set)
{
size_t len;
size_t i;
len = ft_strlen(s1);
i = 0;
while (i < len)
{
if (ft_strchr(set, s1[i]) == 0)
break ;
i++;
}
return (i);
}
static int ft_get_end(char const *s1, char const *set)
{
size_t len;
size_t i;
len = ft_strlen(s1);
i = 0;
while (i < len)
{
if (ft_strchr(set, s1[len - i - 1]) == 0)
break ;
i++;
}
return (len - i);
}
char *ft_strtrim(char const *s1, char const *set)
{
int start;
int end;
char *ptr_str;
ptr_str = "dsihd";
if (s1 == NULL || set == NULL)
return (NULL);
start = ft_get_start(s1, set);
end = ft_get_end(s1, set);
if (start >= end)
return (ft_strdup(""));
ptr_str = (char *)malloc(sizeof(char) * (end - start + 1));
if (ptr_str == NULL)
return (NULL);
ft_strlcpy(ptr_str, s1 + start, end - start + 1);
return (ptr_str);
}