-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
43 lines (40 loc) · 1.41 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
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_strtrim.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: cgarrot <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/10/07 09:01:10 by cgarrot #+# ## ## #+# */
/* Updated: 2018/10/12 16:33:52 by cgarrot ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *d;
int i;
int j;
int tmp;
i = 0;
j = 0;
if (s == 0)
return (NULL);
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
if (*s != 0)
j = ft_strlen(s) - 1;
while (s[j] == ' ' || s[j] == '\n' || s[j] == '\t')
j--;
j++;
if (j - i < 0)
i = 0;
if (!(d = (char*)malloc(sizeof(char) * j - i + 1)))
return (0);
tmp = 0;
while (i < j)
d[tmp++] = s[i++];
d[tmp] = '\0';
return (d);
}