-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
45 lines (40 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aachhoub <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 14:40:15 by aachhoub #+# #+# */
/* Updated: 2022/10/10 09:01:23 by aachhoub ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_strcchr(const char *s, const char c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(const char *s, const char *set)
{
int i;
int j;
if (!s)
return (NULL);
i = 0;
j = ft_strlen(s) - 1;
while (s[i] && ft_strcchr(set, s[i]))
i++;
while (s[j] && ft_strcchr(set, s[j]))
j--;
if (j == -1)
return (ft_substr(s, i, 0));
return (ft_substr(s, i, (j - i + 1)));
}