-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
executable file
·50 lines (46 loc) · 1.45 KB
/
ft_strsplit.c
File metadata and controls
executable file
·50 lines (46 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jspring <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 11:12:36 by jspring #+# #+# */
/* Updated: 2016/05/11 12:45:57 by jspring ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_num_words(char const *s, char c, size_t count, size_t i)
{
while (s[++i])
if (s[i] != c)
{
count++;
while (s[i] != c)
i++;
}
return (count);
}
char **ft_strsplit(char const *s, char c)
{
size_t r;
size_t y;
size_t i;
char **str;
str = (char **)malloc(sizeof(char *) * ft_num_words(s, c, 0, -1) + 1);
i = 0;
r = 0;
while (s[i])
if (s[i] != c)
{
y = i;
while (s[i] != c)
i++;
str[++r - 1] = ft_strsub(s, y, (i - y));
}
else
i++;
str[r] = (char *)malloc(sizeof(char) * 2);
str[r][0] = '\0';
return (str);
}