libft
src
ft_strtrim.c
Go to the documentation of this file.
1
/* ************************************************************************** */
3
/* */
4
/* ::: :::::::: */
5
/* ft_strtrim.c :+: :+: :+: */
6
/* +:+ +:+ +:+ */
7
/* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8
/* +#+#+#+#+#+ +#+ */
9
/* Created: 2019/09/05 11:57:03 by unite #+# #+# */
10
/* Updated: 2020/07/16 02:44:04 by unite ### ########.fr */
11
/* */
12
/* ************************************************************************** */
13
14
#include "
libft.h
"
15
16
static
int
is_trimmable(
char
c)
17
{
18
return
(c ==
' '
|| c ==
'\n'
|| c ==
'\t'
);
19
}
20
34
char
*
ft_strtrim
(
char
const
*s)
35
{
36
size_t
i;
37
size_t
j;
38
39
i = 0;
40
while
(is_trimmable(s[i]))
41
i++;
42
if
(s[i] ==
'\0'
)
43
return
(
ft_strdup
(
""
));
44
j =
ft_strlen
(s) - 1;
45
while
(is_trimmable(s[j]))
46
j--;
47
return
(
ft_strsub
(s, i, j - i + 1));
48
}
ft_strlen
size_t ft_strlen(const char *s)
Replicates behaviour of strlen from libc.
Definition:
ft_strlen.c:20
ft_strdup
char * ft_strdup(const char *s1)
Replicates behaviour of strdup from libc.
Definition:
ft_strdup.c:20
ft_strsub
char * ft_strsub(char const *s, unsigned int start, size_t len)
Copies a substring from a string.
Definition:
ft_strsub.c:28
libft.h
ft_strtrim
char * ft_strtrim(char const *s)
Trims whitespace from the start and end of a string.
Definition:
ft_strtrim.c:34
Generated by
1.8.16