-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
124 lines (113 loc) · 2.27 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
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vstockma <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 09:08:58 by vstockma #+# #+# */
/* Updated: 2022/10/07 09:08:59 by vstockma ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
static int ft_wordcount(const char *str, char c)
{
int i;
int count;
int j;
i = 0;
count = 0;
j = 0;
if (!c)
return (1);
while (str[i])
{
if (str[i] != c)
{
j++;
count++;
i++;
}
while (str[i] && str[i] != c && j > 0)
i++;
while (str[i] == c)
i++;
j = 0;
}
return (count);
}
static char *ft_word(const char *str, char c, int i)
{
char *word;
int j;
int count;
int temp;
j = 0;
temp = i;
count = 0;
while (str[i] && str[i] != c)
{
i++;
count++;
}
word = malloc(sizeof(char) * (count + 1));
if (!word)
return (NULL);
while (j < count)
{
word[j] = str[temp];
j++;
temp++;
}
word[j] = '\0';
return (word);
}
static char **finalstr(char **str, const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
if (s[i] != c)
{
str[j] = ft_word(s, c, i);
j++;
}
while (s[i] != c && s[i])
i++;
while (s[i] == c && s[i])
i++;
}
str[j] = 0;
return (str);
}
char **ft_split(char const *s, char c)
{
char **str;
int arrlen;
if (!s)
return (NULL);
arrlen = ft_wordcount(s, c);
str = malloc(sizeof(char *) * (arrlen + 1));
if (!str)
return (NULL);
return (finalstr(str, s, c));
}
/*int main()
{
const char s[] = " Hallo56 Bro was geht ? ";
char c = ' ';
int i;
char **str;
i = 0;
str = ft_split(s, c);
while (i < 5)
{
printf("%s", str[i]);
i++;
}
return (0);
}*/