-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
41 lines (38 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tluzing <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/31 10:04:16 by tluzing #+# #+# */
/* Updated: 2018/06/04 16:25:11 by tluzing ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
int start;
int end;
int i;
char *new;
if (s)
{
start = 0;
end = ft_strlen((char *)(s)) - 1;
while (s[start] == ' ' || s[start] == '\n' || s[start] == '\t')
start++;
while (s[end] == ' ' || s[end] == '\n' || s[end] == '\t')
end--;
if ((end - start) <= 0)
return (ft_strdup(""));
if (!(new = (char*)malloc(sizeof(*new) * ((end - start) + 2))))
return (NULL);
i = 0;
while (start <= end)
new[i++] = s[start++];
new[i] = '\0';
return (new);
}
return (NULL);
}