-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split_p.c
50 lines (46 loc) · 1.56 KB
/
ft_split_p.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_split_p.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/03 10:19:47 by rbetz #+# #+# */
/* Updated: 2023/04/07 13:41:30 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *set_null(char *s, int i, int j)
{
s[i + 1] = '\0';
return (&s[i - j + 1]);
}
/* this split allocates a new **pointer and expect a allocated input string*/
/* it return pointer to the inputstring*/
char **ft_split_p(char *s, char c)
{
char **ptr;
int i;
int j;
int words;
if (s == NULL)
return (NULL);
words = ft_wordcount(s, c);
ptr = ft_calloc(words + 1, sizeof(char *));
i = ft_strlen(s) - 1;
while (i >= 0 && words > 0 && ptr != NULL)
{
if (s[i] != c)
{
j = 1;
while (j <= i && s[i - j] != c)
j++;
ptr[--words] = set_null(s, i, j);
if (ptr[words] == NULL)
return (free(ptr), NULL);
i = i - j + 1;
}
i--;
}
return (ptr);
}