-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
executable file
·49 lines (45 loc) · 1.54 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasstou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 20:56:33 by mmasstou #+# #+# */
/* Updated: 2021/11/15 10:06:14 by mmasstou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_instring(char c, const char *set)
{
while (*set)
if (c == *set++)
return (1);
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *strtrim;
int index;
int jndex;
int zndex;
if (!s1)
return (NULL);
jndex = ft_strlen(s1);
while (s1[--jndex] && is_instring(s1[jndex], set))
;
index = -1;
while (s1[++index] && is_instring(s1[index], set))
;
if (jndex == -1)
jndex = index;
zndex = jndex - index;
strtrim = (char *)malloc(zndex + 2);
if (!strtrim)
return (NULL);
zndex = index;
while (s1[index] && index <= jndex)
*strtrim++ = s1[index++];
*strtrim = 0;
return (strtrim - (index - zndex));
}