-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
55 lines (49 loc) · 1.39 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: rtiutiun <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/10 20:10:58 by rtiutiun #+# #+# */
/* Updated: 2017/09/21 20:11:00 by rtiutiun ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ws(char c)
{
return (c == ' ' || c == '\n' || c == '\t');
}
static char *ft_strndup(const char *s1, int n)
{
char *dst;
char *p;
dst = ft_strnew(n);
if (!dst)
return (0);
p = dst;
while (*s1 && n > 0)
{
*p++ = *s1++;
n--;
}
*p = 0;
return (dst);
}
char *ft_strtrim(char const *s)
{
int l;
if (!s)
return (NULL);
l = ft_strlen(s);
while (l > 0 && ws(s[l - 1]))
--l;
if (!l)
return (ft_strdup(""));
while (*s && ws(*s))
{
s++;
l--;
}
return (ft_strndup(s, l));
}