-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
51 lines (47 loc) · 1.52 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
50
51
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atilegen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/22 12:29:19 by atilegen #+# #+# */
/* Updated: 2018/03/26 00:04:26 by atilegen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ali(char const *s, int *start, int *last)
{
while (s[*start])
{
if (s[*start] != '\n' && s[*start] != ' ' && s[*start] != '\t')
break ;
*start = *start + 1;
}
while (s[*last] && *last + 1 >= *start)
{
if (s[*last] != '\n' && s[*last] != ' ' && s[*last] != '\t')
break ;
*last = *last - 1;
}
}
char *ft_strtrim(char const *s)
{
int start;
int last;
char *res;
int i;
if (!s)
return (0);
last = ft_strlen(s) - 1;
start = 0;
ali(s, &start, &last);
res = ft_strnew(last - start + 1);
if (!res)
return (NULL);
i = 0;
while (start <= last)
res[i++] = s[start++];
res[i] = '\0';
return (res);
}