-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
53 lines (48 loc) · 1.46 KB
/
ft_strtrim.c
File metadata and controls
53 lines (48 loc) · 1.46 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
52
53
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cmachado <cmachado@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/26 14:19:51 by cmachado #+# #+# */
/* Updated: 2022/03/14 22:05:16 by cmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t index(char const *s1, char const *set, size_t i, int dir)
{
size_t j;
j = 0;
while (set[j] && s1[i])
{
if (s1[i] == set[j])
{
j = 0;
i += dir;
}
else
j++;
}
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *new;
size_t lens1;
size_t start;
if (!set)
{
new = ft_strdup(s1);
return (new);
}
if (!s1)
return (NULL);
lens1 = ft_strlen(s1);
start = index(s1, set, 0, 1);
if (start == lens1)
new = ft_calloc(1, 1);
else
new = ft_substr(s1, start, index(s1, set, lens1 - 1, -1) - start + 1);
return (new);
}