-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
55 lines (51 loc) · 1.54 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
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fay <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 14:35:22 by fay #+# #+# */
/* Updated: 2022/10/18 15:25:24 by fay ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <stdlib.h>
static char *ft_printer(char const *s, unsigned int start, size_t len)
{
char *s1;
int x;
size_t t;
t = ft_strlen(s);
x = 0;
if (start < t)
{
s1 = (char *)malloc((len) * sizeof(char) + 1);
if (!s1)
return (0);
while (s[start] != '\0' && len > 0)
{
s1[x] = s[start];
start++;
len--;
x++;
}
}
else
s1 = malloc(1);
s1[x] = '\0';
return (s1);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *a;
size_t len;
while (*s1 && ft_strchr(set, *s1))
s1++;
len = ft_strlen(s1);
while (ft_strchr(set, s1[len]) && len)
len--;
a = ft_printer((char *)s1, 0, len + 1);
return (a);
}