-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit_at_first_c.c
73 lines (66 loc) · 1.77 KB
/
ft_strsplit_at_first_c.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit_at_first_c.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nvienot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/15 22:18:01 by nvienot #+# #+# */
/* Updated: 2018/12/06 18:28:33 by nvienot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int ft_cnt_len(char const *str, int i)
{
int cnt;
cnt = i;
if (str[cnt] == '\0')
return (1);
while (str[cnt] != '\0')
cnt++;
return (cnt - i);
}
char **ft_rest_after_first_c(char **tab, char const *s, int i)
{
int j;
int k;
j = 1;
k = 0;
if (!(tab[j] = ft_memalloc(ft_cnt_len(s, i))))
return (NULL);
if (s[i] != '\0')
{
i++;
while (s[i] != '\0')
tab[j][k++] = s[i++];
}
tab[j][k] = '\0';
return (tab);
}
char **ft_strsplit_at_first_c(char const *s, char c)
{
int i;
int j;
int k;
char **tab;
i = 0;
j = 0;
k = 0;
if (!s || !(tab = (char**)malloc(sizeof(char*) * 2)))
return (NULL);
while (s[i] && s[i] != c)
i++;
if (!(tab[j] = ft_memalloc(i + 1)))
return (NULL);
i = 0;
while (s[i] && s[i] != c)
{
tab[j][k] = s[i];
k++;
i++;
}
tab[j][k] = '\0';
tab = ft_rest_after_first_c(tab, s, i);
return (tab);
}