-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
40 lines (37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ashongwe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/07 15:29:26 by ashongwe #+# #+# */
/* Updated: 2019/06/14 09:04:09 by ashongwe ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
size_t i;
size_t start;
size_t end;
char *trimmed;
i = 0;
start = 0;
if (!s)
return (NULL);
end = ft_strlen(s);
while (s[start] && ft_isspace(s[start]))
start++;
while (start < end && s[end - 1] && ft_isspace(s[end - 1]))
end--;
if (!(trimmed = (char*)malloc(sizeof(char) * ((end - start) + 1))))
return (NULL);
while (i < (end - start))
{
trimmed[i] = s[start + i];
i++;
}
trimmed[i] = '\0';
return (trimmed);
}