-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
49 lines (45 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
49
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nben-yaa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/06/26 17:55:40 by nben-yaa #+# #+# */
/* Updated: 2018/06/28 01:37:48 by nben-yaa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_space(int c)
{
if (c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == ' '
|| c == '\f')
return (1);
else
return (0);
}
char *ft_strtrim(char const *s)
{
char *res;
int start;
int end;
if (s == 0)
return (NULL);
start = 0;
end = ft_strlen(s);
while (is_space(s[start]) && s[start] != '\0')
start++;
if (start != end)
{
end--;
while (is_space(s[end]) && end >= 0)
{
end--;
}
end++;
}
if ((res = ft_strnew(end - start)) == 0)
return (NULL);
ft_strncpy(res, s + start, end - start);
return (res);
}