-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
44 lines (41 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgavillo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/09 14:43:42 by mgavillo #+# #+# */
/* Updated: 2018/11/24 20:55:06 by mgavillo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>
char *ft_strtrim(char const *s)
{
char *d;
int i;
int j;
int l;
i = 0;
j = (int)ft_strlen(s) - 1;
while (s[j] == ' ' || s[j] == '\n' || s[j] == '\t')
j--;
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
if (j == -1)
return ("");
l = j - i + 1;
if (!(d = malloc((l + 1) * sizeof(char))))
return (NULL);
j = 0;
while (j != l)
{
d[j] = s[i];
j++;
i++;
}
d[j] = '\0';
return (d);
}