-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
37 lines (34 loc) · 1.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwasserf <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/23 13:56:33 by gwasserf #+# #+# */
/* Updated: 2019/06/06 16:23:04 by gwasserf ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(const char *s)
{
char *cursor;
char *newstr;
int index;
if (!s)
return (NULL);
cursor = (char *)s;
while (ft_isspace(*cursor))
cursor++;
if (!*cursor)
return (ft_strnew(0));
index = ft_strlen(cursor);
while (ft_isspace(cursor[index - 1]))
index--;
newstr = ft_strnew(index);
if (!newstr)
return (NULL);
ft_strncpy(newstr, cursor, index + 1);
newstr[index] = 0;
return (newstr);
}