-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
executable file
·55 lines (51 loc) · 1.65 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mframbou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/06 16:50:36 by mframbou #+# #+# */
/* Updated: 2021/08/08 19:03:12 by mframbou ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static int ft_isinset(char c, char const *set)
{
while (*set)
{
if (*set++ == c)
return (1);
}
return (0);
}
// " T "
// ^
// start & end on char T (=> size 0 => wrong)
// ==> So always check for character before end "cursor" to get the right size
// " "
// ^ ^
// END START
// (Also handle case where string if full of whitespaces)
char *ft_strtrim(char const *s, char const *set)
{
size_t start;
size_t end;
char *res;
start = 0;
end = 0;
while (ft_isinset(s[start], set))
start++;
while (s[end])
end++;
while (ft_isinset(s[end - 1], set) && end != 0 && end > start)
end--;
res = (char *) malloc (sizeof(char) * (end - start + 1));
if (res)
{
res[end - start] = '\0';
while (end-- > start)
res[end - start] = s[end];
}
return (res);
}