-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
48 lines (44 loc) · 1.44 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
45
46
47
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rymuller <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/26 15:07:56 by rymuller #+# #+# */
/* Updated: 2018/11/29 20:29:46 by rymuller ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int iswhitespace(char c)
{
if (c == ' ' || c == '\n' || c == '\t')
return (1);
return (0);
}
char *ft_strtrim(char const *s)
{
char *mal;
size_t i;
size_t start;
size_t end;
if (s != NULL)
{
start = 0;
while (iswhitespace(s[start]))
start++;
end = ft_strlen(s) - 1;
if (start == end + 1)
return (ft_strnew(0));
while (iswhitespace(s[end]))
end--;
if (!(mal = (char *)malloc(end - start + 2)))
return (NULL);
i = 0;
while (start <= end)
mal[i++] = s[start++];
mal[i] = '\0';
return (mal);
}
return (NULL);
}