-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
executable file
·41 lines (38 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oltkache <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 20:24:01 by oltkache #+# #+# */
/* Updated: 2017/10/30 20:24:02 by oltkache ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *res;
int i;
int j;
size_t len;
if (!s)
return (NULL);
i = 0;
len = ft_strlen(s);
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
++i;
if (i == len)
return (ft_strdup(""));
while (s[len - 1] == ' ' || s[len - 1] == '\n' || s[len - 1] == '\t')
--len;
len -= i;
res = (char*)malloc(sizeof(char) * (len + 2));
if (res == NULL)
return (NULL);
j = 0;
while (j < len)
res[j++] = s[i++];
res[j] = '\0';
return (res);
}