-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
46 lines (42 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cprune <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 11:40:53 by cprune #+# #+# */
/* Updated: 2015/12/14 14:00:18 by cprune ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>
static int ft_istrimspace(int c)
{
return ((c == ' ' || c == '\n' || c == '\t' ? 1 : 0));
}
char *ft_strtrim(char const *s)
{
int len;
int cmpe;
char *str;
cmpe = 0;
while (ft_istrimspace(*s))
s++;
if (*s == '\0')
return ("");
len = strlen(s);
s = s + len;
while (ft_istrimspace(*--s))
cmpe++;
if(!(str = (char *)malloc(sizeof(char) * (len - cmpe + 1))))
return (NULL);
str[len - cmpe] = '\0';
while (len - cmpe - 1 >= 0)
{
str[len - cmpe - 1] = *s--;
len--;
}
return (str);
}