-
Notifications
You must be signed in to change notification settings - Fork 0
/
_strtok.c
94 lines (91 loc) · 1.51 KB
/
_strtok.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
#include "sshell.h"
/**
* _sch - search if a char is inside a string
* @s: string to review
* @c: char to find
* Return: 1 if success 0 if not
*/
int _sch(char *s, char c)
{
int cont = 0;
while (s[cont] != '\0')
{
if (s[cont] == c)
{
break;
}
cont++;
}
if (s[cont] == c)
return (1);
else
return (0);
}
/**
* _strtok - function that cut a string into tokens depending of the delimit
* @s: string to cut in parts
* @d: delimiters
* Return: first partition
*/
char *_strtok(char *s, char *d)
{
static char *ultimo;
int i = 0, j = 0;
if (!s)
s = ultimo;
while (s[i] != '\0')
{
if (_sch(d, s[i]) == 0 && s[i + 1] == '\0')
i++;
else if (_sch(d, s[i]) == 0 && _sch(d, s[i + 1]) == 0)
i++;
else if (_sch(d, s[i]) == 0 && _sch(d, s[i + 1]) == 1)
{
ultimo = s + i + 1;
*ultimo = '\0';
ultimo++;
s = s + j;
return (s);
}
else if (_sch(d, s[i]) == 1)
{
j++;
i++;
}
}
return (NULL);
}
/**
* _strtok2 - function tokenizaition with ;
* @s: string to cut in parts
* @d: delimiters
* Return: first partition
*/
char *_strtok2(char *s, char *d)
{
static char *ultimo;
int i = 0, j = 0;
if (!s)
s = ultimo;
while (s[i] != '\0')
{
if (_sch(d, s[i]) == 0 && s[i + 1] == '\0')
i++;
else if (_sch(d, s[i]) == 0 && _sch(d, s[i + 1]) == 0)
i++;
else if (_sch(d, s[i]) == 0 && _sch(d, s[i + 1]) == 1)
{
ultimo = s + i + 1;
*ultimo = '\0';
ultimo++;
s = s + j;
return (s);
}
else if (_sch(d, s[i]) == 1)
{
j++;
i++;
}
}
return (NULL);
}