-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_utils4.c
87 lines (78 loc) · 1.19 KB
/
ft_utils4.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
#include "minishell.h"
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
i++;
return (s1[i] - s2[i]);
}
int count_word(const char *str, char c)
{
int i;
int word;
word = 0;
i = 0;
while (str[i])
{
while (str[i] && str[i] == c)
i++;
if (str[i] && str[i] != c)
{
word++;
while (str[i] && str[i] != c)
i++;
}
}
return (word);
}
char *tab_malloc(const char *str, char c)
{
int i;
char *tab;
i = 0;
while (str[i] && str[i] != c)
i++;
tab = (char *)malloc(sizeof(char) * i + 1);
if (tab == NULL)
return (NULL);
i = 0;
while (str[i] && str[i] != c)
{
tab[i] = str[i];
i++;
}
tab[i] = '\0';
return (tab);
}
char **ft_split(char const *s, char c)
{
int i;
int i_tab;
int total_word;
char **tab;
i = 0;
i_tab = 0;
total_word = count_word(s, c);
tab = (char **)malloc(sizeof(char *) * total_word + 1);
if (tab == NULL)
return (NULL);
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] && s[i] != c)
{
tab[i_tab] = tab_malloc(s + i, c);
i_tab++;
while (s[i] && s[i] != c)
i++;
}
}
tab[i_tab] = 0;
return (tab);
}
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}