-
Notifications
You must be signed in to change notification settings - Fork 0
/
string3.c
92 lines (83 loc) · 2.01 KB
/
string3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebennamr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 17:53:34 by ebennamr #+# #+# */
/* Updated: 2023/02/14 16:59:46 by ebennamr ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static size_t count_words(char const *string, char c)
{
size_t count;
int i;
count = 0;
i = 0;
while (string[i] == c)
i++;
while (string[i])
{
if (!string[i + 1] || (string[i] == c && string[i + 1] != c))
{
count++;
}
i++;
}
if (count == 0)
ft_error(ERROR);
return (count);
}
char **ft_split(char *s, char c)
{
char **split;
size_t i;
size_t count;
size_t save;
i = 0;
count = 0;
if (!s)
return (0);
split = (char **)malloc(sizeof(char *) * (count_words(s, c) + 1));
if (!split)
return (0);
while (i < count_words(s, c) && s[count] != '\0')
{
while (s[count] == c)
count++;
save = count;
while (s[count] != c && s[count] != '\0')
count++;
split[i] = ft_substr(&s[save], 0, count - save);
if (split[i++] == 0)
ft_error(ERROR);
}
split[i] = 0;
return (split);
}
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] && s2[i])
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
i++;
}
return (s1[i] - s2[i]);
}
void ft_bzero(void *pt, int n)
{
int i;
unsigned char *str;
i = 0;
str = (unsigned char *)(pt);
while (i < n)
{
str[i] = 0;
i ++;
}
}