-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
59 lines (52 loc) · 1.68 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hnoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/04 10:50:40 by hnoh #+# #+# */
/* Updated: 2021/01/04 11:28:58 by nogeun ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_possible(char c, char const *set)
{
int count;
count = -1;
while (set[++count])
if (set[count] == c)
return (1);
return (0);
}
static int ft_get_size(char const *s1, char const *set)
{
int count;
int size;
size = ft_strlen(s1);
count = 0;
while (ft_possible(s1[size - count - 1], set))
count++;
return (size - count);
}
char *ft_strtrim(char const *s1, char const *set)
{
int count;
int size;
char *tab;
count = 0;
size = 0;
if (!s1)
return (0);
if (!set)
return (ft_strdup(s1));
while (ft_possible(s1[count], set))
count++;
if (count == (int)ft_strlen(s1))
return (ft_strdup(""));
size = ft_get_size(s1 + count, set) + 1;
if (!(tab = (char *)malloc((size) * sizeof(char))))
return (NULL);
ft_strlcpy(tab, s1 + count, size);
return (tab);
}