-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
58 lines (52 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yochered <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/27 13:52:56 by yochered #+# #+# */
/* Updated: 2018/10/27 13:52:57 by yochered ### ########.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
static int is_space(char c)
{
if (c == ' ' || c == ',' || c == '\n' || c == '\t')
return (1);
return (0);
}
static int count_len(char const *s)
{
int len;
int i;
len = ft_strlen(s);
while (is_space(s[len - 1]))
len--;
i = -1;
while (is_space(s[++i]))
len--;
if (len < 0)
len = 0;
return (len);
}
char *ft_strtrim(char const *s)
{
int i;
int len;
char *res;
if (!s)
return (NULL);
i = -1;
len = count_len(s);
res = (char *)malloc((len + 1) * sizeof(char));
if (!res)
return (NULL);
while (is_space(*s))
s++;
while (++i < len)
res[i] = *s++;
res[i] = '\0';
return (res);
}