-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
42 lines (39 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvenance <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/12 13:34:55 by vvenance #+# #+# */
/* Updated: 2016/01/13 16:45:44 by vvenance ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(const char *s)
{
char *nstr;
size_t i;
size_t j;
if (s == NULL || !s)
return (char *)(s);
i = 0;
j = 0;
if ((nstr = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1))) == NULL)
return (NULL);
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
while (s[i] != '\0')
{
nstr[j] = s[i];
j++;
i++;
}
while (nstr[j - 1] == ' ' || nstr[j - 1] == '\n' || nstr[j - 1] == '\t')
{
nstr[j - 1] = '\0';
j--;
}
nstr[j] = '\0';
return (nstr);
}