-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
74 lines (66 loc) · 1.79 KB
/
Copy pathft_strtrim.c
File metadata and controls
74 lines (66 loc) · 1.79 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ophuong <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/12 12:31:03 by ophuong #+# #+# */
/* Updated: 2019/09/18 13:28:19 by ophuong ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int f_cutstart(const char *s, unsigned long id)
{
while (ft_isspace(s[id]) == 1 && s[id] != '\0')
id++;
return (id);
}
static int f_cutend(const char *s)
{
unsigned int len;
len = ft_strlen(s);
if (len == 0)
return (1);
while (ft_isspace(s[len]) == 1 || s[len] == '\0')
{
len--;
if (len == 0)
break ;
}
return (len);
}
static char *f_trim(const char *s, unsigned long id, unsigned long len)
{
char *fresh;
unsigned long od;
od = 0;
fresh = ft_strnew((len + 1 - id));
if (!fresh)
return (NULL);
while (id < len + 1)
{
fresh[od] = s[id];
id++;
od++;
}
fresh[od] = '\0';
return (fresh);
}
char *ft_strtrim(char const *s)
{
char *fresh;
unsigned long id;
unsigned long len;
id = 0;
len = 0;
fresh = NULL;
if (s == NULL)
return (NULL);
len = f_cutend(s);
id = f_cutstart(s, id);
if (len < id)
len = id;
fresh = f_trim(s, id, len);
return (fresh);
}