-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
52 lines (47 loc) · 1.54 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
52
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/18 12:50:21 by dskrypny #+# #+# */
/* Updated: 2017/11/18 15:05:53 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_cut(char *s)
{
int i;
i = 0;
while (s[i] != ' ' && s[i] != '\t' && s[i] != '\n')
i++;
s[i] = 0;
return (s);
}
char *ft_strtrim(char const *s)
{
char *res;
char *res1;
int i;
int begin;
if (s == NULL)
return (NULL);
if (!(res = (char *)malloc(sizeof(char) * (ft_strlen(s) + 1))))
return (NULL);
i = 0;
while (s[i] == ' ' || s[i] == '\t' || s[i] == '\n')
i++;
begin = i;
i--;
while (s[i++])
res[i - begin] = s[i];
i -= 2;
while (s[i] == ' ' || s[i] == '\t' || s[i] == '\n')
i--;
if (!(res1 = (char *)malloc(sizeof(char) * (i - begin + 1))))
return (NULL);
res1 = ft_cut(res);
free(res);
return (res1);
}