-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
79 lines (68 loc) · 1.82 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
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: albarret <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/12 17:28:40 by albarret #+# #+# */
/* Updated: 2018/11/13 00:31:52 by albarret ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_len_no_esc(int start, int end)
{
while (start < end)
start++;
return (start);
}
static int start(char const *s)
{
int i;
i = 0;
while (s[i] == ' ' || s[i] == '\t' || s[i] == '\n')
i++;
return (i);
}
static int end(char const *s)
{
int i;
int slen;
i = 0;
slen = (ft_strlen(s));
while (s[slen] == ' ' || s[slen] == '\t' || s[slen] == '\n')
slen--;
return (slen);
}
char *ft_strtrim(char const *s)
{
int start_len;
int end_len;
char *mem;
char const *sptr;
int i;
i = 0;
sptr = (char*)s;
start_len = start(s);
end_len = end(s);
if (!(s || !(mem = malloc(sizeof(char) * (ft_len_no_esc(start_len, end_len)) + 1))))
return (NULL);
while (start_len < end_len)
{
mem[i] = sptr[start_len];
start_len++;
i++;
}
mem[i] = '\0';
return (mem);
}
int main(void)
{
char *mem;
char *ptr;
ptr = " thisismystring ";
ft_strtrim(ptr);
mem = ft_strtrim(ptr);
printf("%s\n", mem);
return (0);
}