-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
42 lines (39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kamako <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/15 13:05:54 by kamako #+# #+# */
/* Updated: 2019/06/19 13:49:36 by kamako ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
unsigned int i;
unsigned int k;
unsigned int j;
char *str;
i = 0;
k = 0;
if (!s)
return (NULL);
while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
i++;
if (s[i] == '\0')
return (ft_strcpy(ft_memalloc(sizeof(char) * 2), ""));
j = ft_strlen(s) - 1;
while (s[j] == ' ' || s[j] == '\n' || s[j] == '\t')
j--;
if ((str = (char *)malloc(sizeof(char) * (j - i + 2))) == NULL)
return (NULL);
while (k < j - i + 1)
{
str[k] = s[i + k];
k++;
}
str[k] = '\0';
return (str);
}