-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_strtrim.c
36 lines (32 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 14:24:03 by smbaabu #+# #+# */
/* Updated: 2019/02/27 01:36:13 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_is_space(char c)
{
return ((c == '\n' || c == '\t' || c == ' '));
}
char *ft_strtrim(char const *s)
{
size_t start;
size_t end;
if (!s)
return (NULL);
start = 0;
while (s[start] && ft_is_space(s[start]))
start++;
end = ft_strlen(s) - 1;
while (end > start && ft_is_space(s[end]))
end--;
if (end - start == 0)
return (ft_strdup(s));
return (ft_strsub(s, start, end - start + 1));
}