-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
52 lines (47 loc) · 1.49 KB
/
Copy pathft_strtrim.c
File metadata and controls
52 lines (47 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjungoo <kjungoo@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/12 19:23:29 by kjungoo #+# #+# */
/* Updated: 2022/07/28 18:28:15 by kjungoo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char is_char_in_set(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *str;
size_t head;
size_t tail;
size_t i;
if (s1 == 0 || set == 0)
return (0);
head = 0;
while (s1[head] && is_char_in_set(s1[head], set))
head++;
tail = ft_strlen(s1);
while (head < tail && is_char_in_set(s1[tail - 1], set))
tail--;
str = (char *)malloc(tail - head + 1);
if (str == 0)
return (0);
i = 0;
while (head < tail)
str[i++] = s1[head++];
str[i] = '\0';
return (str);
}