-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
52 lines (47 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaraval <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/14 08:21:11 by tmaraval #+# #+# */
/* Updated: 2017/11/20 08:13:03 by tmaraval ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_skip_space(char const *s)
{
int stt;
stt = 0;
while (s[stt] && (s[stt] == ' ' || s[stt] == '\t' || s[stt] == '\n'))
stt++;
return (stt);
}
char *ft_strtrim(char const *s)
{
size_t stt;
size_t end;
int i;
char *ret;
if (s == NULL)
return (NULL);
i = -1;
end = ft_strlen(s) - 1;
stt = ft_skip_space(s);
if (stt == end + 1)
{
if ((ret = (char *)malloc(sizeof(char) * 1)) == NULL)
return (NULL);
*ret = '\0';
return (ret);
}
while (end > 0 && (s[end] == ' ' || s[end] == '\t' || s[end] == '\n'))
end--;
if ((ret = (char *)malloc(sizeof(char) * (end - stt) + 2)) == NULL)
return (NULL);
while (stt + ++i <= end)
ret[i] = s[stt + i];
ret[i] = '\0';
return (ret);
}