-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
69 lines (63 loc) · 1.64 KB
/
ft_split.c
File metadata and controls
69 lines (63 loc) · 1.64 KB
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: ychihab <ychihab@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 11:35:30 by ychihab #+# #+# */
/* Updated: 2022/10/24 08:17:59 by ychihab ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *str, char sep)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
while (str[i] && str[i] == sep)
i++;
if (str[i])
{
while (str[i] && str[i] != sep)
i++;
count++;
}
}
return (count);
}
static char **ft_split_no_pro(char const *s, char c)
{
int i;
int j;
char **ptr;
int k;
i = 0;
k = 0;
ptr = malloc(sizeof(char *) * (ft_count_words(s, c) + 1));
if (!ptr)
return (0);
while (k < ft_count_words(s, c))
{
while (s[i] && s[i] == c)
i++;
j = 0;
while (s[i] && s[i] != c)
{
i++;
j++;
}
ptr[k++] = ft_substr(s, i - j, j);
}
ptr[k] = NULL;
return (ptr);
}
char **ft_split(char const *s, char c)
{
if (s == 0)
return (0);
return (ft_split_no_pro(s, c));
}