-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_functions.c
85 lines (80 loc) · 1.37 KB
/
str_functions.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
#include "monty.h"
/**
* _strcmp - Function that compares two strings.
* @s1: type str compared
* @s2: type str compared
* Return: 0 if s1 and s2 are equals.
* another value if they are different
*/
int _strcmp(char *s1, char *s2)
{
int i;
for (i = 0; s1[i] == s2[i] && s1[i]; i++)
;
if (s1[i] > s2[i])
return (1);
if (s1[i] < s2[i])
return (-1);
return (0);
}
/**
* _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);
}
/**
* _strtoky - function that cut a string into tokens depending of the delimit
* @s: string to cut in parts
* @d: delimiters
* Return: first partition
*/
char *_strtoky(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')
{
ultimo = s + i + 1;
*ultimo = '\0';
s = s + j;
return (s);
}
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);
}