-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
93 lines (84 loc) · 2.13 KB
/
ft_split.c
File metadata and controls
93 lines (84 loc) · 2.13 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muhozkan <muhozkan@student.42.tr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/19 16:15:11 by muhozkan #+# #+# */
/* Updated: 2022/02/25 23:35:28 by muhozkan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
if (s[i] == c)
i++;
else
{
count++;
while (s[i] && s[i] != c)
i++;
}
}
return (count);
}
static char *ft_make_words(char *word, char const *s, int j, int word_ln)
{
int i;
i = 0;
while (word_ln > 0)
word[i++] = s[j - word_ln--];
word[i] = 0;
return (word);
}
static char **ft_split_words(char **res, char const *s, char c, int word_ct)
{
int i;
int j;
int word_ln;
i = 0;
j = 0 ;
word_ln = 0;
while (s[j] && i < word_ct)
{
while (s[j] && s[j] == c)
j++;
while (s[j] && s[j] != c)
{
j++;
word_ln++;
}
res[i] = (char *)malloc(sizeof(char) * (word_ln + 1));
if (!res[i])
return (0);
ft_make_words (res[i], s, j, word_ln);
word_ln = 0;
i++;
}
res[i] = 0;
return (res);
}
char **ft_split(char const *s, char c)
{
int word_ct;
char **res;
if (s == 0)
return (0);
word_ct = ft_count_words(s, c);
res = (char **)malloc(sizeof(char *) * (word_ct + 1));
if (!res)
return (0);
ft_split_words (res, s, c, word_ct);
return (res);
}
/*
her elemanı yazdırmasını burada a yı arttırarak sağladık.
böyle yazmazsak sadece fatma yazıp bıraktı
*/