-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
40 lines (36 loc) · 1.31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alaulom <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/13 14:39:30 by alaulom #+# #+# */
/* Updated: 2014/11/13 15:11:23 by alaulom ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static unsigned int ft_sp(char c)
{
return (c == ' ' || c == '\t' || c == '\n');
}
char *ft_strtrim(const char *s)
{
char *dst;
size_t i;
size_t e;
if (!s)
return (NULL);
i = 0;
while (ft_sp(*s++))
i++;
s -= i + 1;
e = ft_strlen(s) - 1;
if (!*s || e == i - 1)
return (dst = ft_strnew(1));
if (i != (e + 1))
while (ft_sp(s[e]))
e--;
dst = ft_strsub(s, i, (e - i + 1));
return (dst);
}