-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
40 lines (37 loc) · 1.34 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: ssalaues <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 13:41:02 by ssalaues #+# #+# */
/* Updated: 2016/12/12 19:20:29 by ssalaues ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *str;
size_t i1;
size_t i2;
if (!s)
return (NULL);
i1 = 0;
i2 = ft_strlen(s) - 1;
while (s[i1] == ' ' || s[i1] == '\n' || s[i1] == '\t')
i1++;
while (i2 > 0 && (s[i2] == ' ' || s[i2] == '\n' || s[i2] == '\t'))
i2--;
if (i2 < i1)
{
if (!(str = (char *)malloc(1)))
return (NULL);
*str = '\0';
return (str);
}
str = ft_strsub(s, i1, i2 - i1 + 1);
if (!str)
return (NULL);
return (str);
}