-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
69 lines (62 loc) · 1.59 KB
/
ft_split.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: havyilma <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 17:45:20 by havyilma #+# #+# */
/* Updated: 2022/10/21 17:07:40 by havyilma ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_row(const char *s, char c)
{
int count;
count = 0;
while (*s)
{
while (*s == c)
s++;
if (*s)
count++;
while (*s != c && *s)
s++;
}
return (count);
}
int ft_col(const char *s, char c)
{
int i;
i = 0;
while (s[i] != c && s[i])
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
char **tab;
int i;
int k;
int a;
i = 0;
if (!s)
return (NULL);
tab = (char **)malloc(sizeof(char *) * (ft_row(s, c) + 1));
if (!tab)
return (0);
a = ft_row(s, c);
while (i < a)
{
k = 0;
tab[i] = malloc(sizeof(char) * ft_col(s, c) + 1);
while (*s == c)
s++;
while (*s != c && *s)
tab[i][k++] = *s++;
tab[i][k] = 0;
i++;
}
tab[i] = 0;
return (tab);
}