-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_to_k.c
65 lines (61 loc) · 1.03 KB
/
str_to_k.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
#include "main.h"
/**
* contains_char - checks if a string contains a character.
* @c: character to check
* @str: string to check
* Return: 1 on Success, 0 on failure.
*/
unsigned int contains_char(char c, const char *str)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++)
{
if (c == str[i])
return (1);
}
return (0);
}
/**
* _strtok - split string into tokens
* @str: the string to split
* @delim: the delimiter
* Return: a pointer to the Next token or NULL
*/
char *_strtok(char *str, const char *delim)
{
static char *ts;
static char *nt;
unsigned int i;
if (str != NULL)
nt = str;
ts = nt;
if (ts == NULL)
return (NULL);
for (i = 0; ts[i] != '\0'; i++)
{
if (!contains_char(ts[i], delim))
break;
}
if (nt[i] == '\0' || nt[i] == '#')
{
nt = NULL;
return (NULL);
}
ts = nt + i;
nt = ts;
for (i = 0; nt[i] != '\0'; i++)
{
if (contains_char(nt[i], delim))
break;
}
if (nt[i] == '\0')
nt = NULL;
else
{
nt[i] = '\0';
nt = nt + i + 1;
if (*nt == '\0')
nt = NULL;
}
return (ts);
}