-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
115 lines (103 loc) · 2.32 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 11:40:26 by juhtoo-h #+# #+# */
/* Updated: 2024/08/30 11:36:03 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_countletter(char const *s, char c, int pos)
{
int count;
count = 0;
while (s[pos] != c && s[pos])
{
count++;
pos++;
}
return (count);
}
static int ft_wordcount(char const *s, char c)
{
int count;
count = 0;
while (*s)
{
while (*s == c)
s++;
if (*s)
count++;
while (*s && *s != c)
s++;
}
return (count);
}
static char *word_generator(char const *s, char c, int pos)
{
int len;
int i;
char *word;
len = ft_countletter(s, c, pos);
word = (char *)malloc(len + 1);
if (word == NULL)
return (NULL);
i = 0;
while (s[pos + i] != c && s[pos + i])
{
word[i] = s[pos + i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
int i;
int pos;
int size;
char **lst;
if (!s)
return (NULL);
size = ft_wordcount(s, c);
lst = (char **)malloc(sizeof(char *) * (size + 1));
if (lst == NULL)
return (NULL);
i = 0;
pos = 0;
while (s[pos])
{
if (s[pos] != c && s[pos])
{
lst[i] = word_generator(s, c, pos);
pos = pos + ft_countletter(s, c, pos) - 1;
i++;
}
pos++;
}
lst[i] = 0;
return (lst);
}
// #include <stdio.h>
// int main(void)
// {
// char str[500] = " tripouille 42 ";
// char c = ' ';
// int i;
// char **arr;
// int j;
// j = 0;
// i = ft_wordcount(str, c);
// printf("%d\n", i);
// arr = ft_split(str, c);
// while (j <= i)
// {
// printf("%s\n", arr[j]);
// free(arr[j]);
// j++;
// }
// free(arr);
// }