-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
50 lines (46 loc) · 1.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jsprouts <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/24 21:46:30 by jsprouts #+# #+# */
/* Updated: 2019/09/24 23:00:27 by jsprouts ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_min_max(char const *s, int *min, int *max)
{
while (s[*min] == ' ' || s[*min] == '\n' || s[*min] == '\t')
*min += 1;
while ((s[*max] == ' ' || s[*max] == '\n' || s[*max] == '\t')
&& *min < *max)
*max -= 1;
}
char *ft_strtrim(char const *s)
{
int min;
int max;
int i;
char *str;
i = 0;
if (!s)
return (NULL);
if ((max = ft_strlen(s)) == 0)
{
if (!(str = (char*)malloc(sizeof(char) * 1)))
return (NULL);
*str = 0;
return (str);
}
max--;
min = 0;
ft_min_max(s, &min, &max);
if (!(str = (char*)malloc(sizeof(char) * (max + 2 - min))))
return (NULL);
while (min <= max)
str[i++] = s[min++];
str[i] = 0;
return (str);
}