Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
104 lines (93 loc) · 2.24 KB
/
Copy pathft_split.c
File metadata and controls
104 lines (93 loc) · 2.24 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asalniko <asalniko@student.42berlin.d +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/17 16:25:03 by asalniko #+# #+# */
/* Updated: 2025/04/04 15:04:58 by asalniko ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_word_counter(const char *s, char c)
{
size_t word_counter;
if (! *s)
return (0);
word_counter = 0;
while (*s)
{
if (*s != c && (*(s + 1) == c || *(s + 1) == '\0'))
word_counter++;
s++;
}
return (word_counter);
}
char *ft_worddup(const char *str, char c)
{
const char *start;
char *word;
size_t len;
size_t i;
start = str;
len = 0;
i = 0;
while (str[len] && str[len] != c)
len++;
word = malloc(sizeof(char) * (len + 1));
if (!word)
return (NULL);
while (i < len)
{
word[i] = start[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char const *s, char c)
{
char **splitted;
int i;
int words;
if (!s)
return (NULL);
words = ft_word_counter(s, c);
splitted = malloc(sizeof(char *) * (words + 1));
if (!splitted)
return (NULL);
i = 0;
while (i < words)
{
while (*s && *s == c)
s++;
splitted[i] = ft_worddup(s, c);
while (*s && *s != c)
s++;
i++;
}
splitted[i] = NULL;
return (splitted);
}
/*
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char **result = ft_split("Hello,World,42,School", ',');
int i = 0;
if (!result)
{
printf("memory allocation failed\n");
return (1);
}
while (result[i])
{
printf("substring %d: %s\n", i, result[i]);
free(result[i]);
i++;
}
free(result);
return (0);
}*/