-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (77 loc) · 1.93 KB
/
Copy pathft_split.c
File metadata and controls
84 lines (77 loc) · 1.93 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: devoma <devoma@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/18 12:18:53 by devoma #+# #+# */
/* Updated: 2023/05/18 18:02:05 by devoma ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int char_count(char *s, char c)
{
int count;
int i;
int len;
i = 0;
count = 1;
len = ft_strlen(s) - 1;
while (s[i] && s[i] == c)
i++;
while (s[len] && s[len] == c && i < len)
len--;
if (i == ft_strlen(s))
return (0);
while (s[i] && i < len)
{
if (s[i] == c && s[i - 1] != c)
count++;
i++;
}
return (count);
}
char *create_word(char *s, int start, int len)
{
char *word;
int i;
i = 0;
word = malloc(sizeof(char) * (len + 1));
if (!word)
return (NULL);
while (i < len)
{
word[i] = s[start + i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char *s, char c)
{
int start;
int end;
int i;
int quantity;
char **result;
start = 0;
i = 0;
quantity = char_count(s, c);
result = malloc(sizeof(char *) * (quantity + 1));
if (!s || !result)
return (NULL);
while (i < quantity)
{
while (s[start] && s[start] == c)
start++;
end = start;
while (s[end] && s[end] != c)
end++;
result[i] = create_word(s, start, end - start);
start = end;
i++;
}
result[i] = NULL;
return (result);
}