-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
67 lines (60 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbopp <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 11:26:56 by lbopp #+# #+# */
/* Updated: 2017/01/20 12:54:22 by lbopp ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_size(char const *s, int start)
{
int i;
i = start;
while (s[i])
i++;
while (ft_isspace(s[i - 1]) && (i > 0))
{
i--;
}
if ((i - start) < 0)
return (0);
else
return (i - start);
}
static char *ft_news(const char *s, int start, int len, char *news)
{
int i;
i = 0;
while (start < len)
{
news[i] = s[start];
start++;
i++;
}
news[i] = '\0';
return (news);
}
char *ft_strtrim(char const *s)
{
char *news;
int i;
int start;
int j;
int size;
i = 0;
j = 0;
if (!s)
return (0);
while (s[i] && ft_isspace(s[i]))
i++;
start = i;
size = ft_size(s, start);
if (!(news = (char*)malloc(sizeof(char) * (size + 1))))
return (0);
news = ft_news(s, start, size + start, news);
return (news);
}