-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
42 lines (39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: msaliuta <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/04 16:34:02 by msaliuta #+# #+# */
/* Updated: 2018/11/04 16:34:03 by msaliuta ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char const *start;
char const *end;
char *result;
start = NULL;
if (s == NULL)
return (NULL);
while (*s)
{
if (!(*s == ' ' || *s == '\n' || *s == '\t'))
{
start = (start == NULL) ? s : start;
end = s;
}
s++;
}
if (start == NULL)
return (ft_strnew(0));
if ((result = (char *)malloc(sizeof(char) * (end - start + 2))) == NULL)
return (NULL);
s = start;
while (s <= end)
*result++ = *s++;
*result = '\0';
return (result - (end - start + 1));
}