-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
88 lines (81 loc) · 2.05 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amennad <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/18 17:41:22 by amennad #+# #+# */
/* Updated: 2023/04/21 10:02:01 by amennad ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <stdlib.h>
int nb_str(char *str, char c)
{
int i;
int nb_word;
i = 0;
nb_word = 0;
while (str[i] != '\0')
{
if (str[i] != c && (str[i + 1] == c || str[i + 1] == '\0'))
{
nb_word++;
}
i++;
}
return (nb_word);
}
void testfac(char *str, char sep, int tab_size, char **tab)
{
int i;
int startword;
int len_world;
int j;
j = 0;
i = 0;
while (str[i] != '\0')
{
len_world = 0;
if (str[i] == sep)
i++;
startword = i;
if (str[i] != sep)
{
while (str[i] != sep && str[i] != '\0')
{
len_world++;
if ((str[i + 1] == sep || str[i + 1] == '\0') && j < tab_size)
tab[j++] = ft_substr(str, startword, len_world);
i++;
}
}
}
tab[j] = (NULL);
}
char **ft_split(char const *s, char sep)
{
int tab_size;
char *str;
char **tab;
char **error;
str = (char *)s;
if (!str)
{
error = (char **)ft_calloc(1, sizeof(char *));
return (error);
}
tab_size = nb_str(str, sep);
if (tab_size == 0)
{
error = (char **)ft_calloc(1, sizeof(char *));
return (error);
}
tab = (char **)ft_calloc(tab_size + 1, sizeof(char *));
if (!tab)
return (NULL);
testfac(str, sep, tab_size, tab);
return (tab);
}