-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
executable file
·57 lines (52 loc) · 1.61 KB
/
ft_strsplit.c
File metadata and controls
executable file
·57 lines (52 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: frcugy <frcugy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/03 11:18:50 by frcugy #+# #+# */
/* Updated: 2014/11/03 11:18:50 by frcugy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_wordscount(char *s, char c)
{
size_t nb;
while (*s && *s == c)
s++;
nb = (*s ? 1 : 0);
while (*s)
{
if (*s == c && *(s + 1) && *(s + 1) != c)
nb++;
s++;
}
return (nb);
}
char **ft_strsplit(char const *s, char c)
{
size_t words;
char *start;
char **result;
if (s == NULL)
return (NULL);
words = ft_wordscount((char *)s, c);
if (!(result = (char **)malloc(sizeof(char *) * (words + 1))))
return (NULL);
start = (char *)s;
while (*s)
{
if (*s == c)
{
if (start != s)
*(result++) = ft_strsub(start, 0, s - start);
start = (char *)s + 1;
}
s++;
}
if (start != s)
*(result++) = ft_strsub(start, 0, s - start);
*result = NULL;
return (result - words);
}