-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtok.c
93 lines (82 loc) · 1.39 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
#include "shell.h"
#define _ISOC99_SOURCE
/**
* is_delim - check if delimter.
* @c: char to check.
* @delim: the delimiter.
* Return: if delim gives 1, if not give 0.
*/
unsigned int is_delim(char c, char *delim)
{
while (*delim != '\0')
{
if (c == *delim)
return (1);
delim++;
}
return (0);
}
/**
* _strtok - split string.
* @srcString: string to split.
* @delim: separator.
* Return: splited strings.
*/
char *_strtok(char *srcString, char *delim)
{
static char *backup_string;
char *ret;
if (!srcString)
srcString = backup_string;
if (!srcString)
return (NULL);
while (1)
{
if (is_delim(*srcString, delim))
{
srcString++;
continue;
}
if (*srcString == '\0')
return (NULL);
break;
}
ret = srcString;
while (1)
{
if (*srcString == '\0')
{
backup_string = srcString;
return (ret);
}
if (is_delim(*srcString, delim))
{
*srcString = '\0';
backup_string = srcString + 1;
return (ret);
}
srcString++;
}
}
/**
* split_string - check code.
* @str: string to split.
* @delimiter: charactaer.
* Return: splited string.
*/
char **split_string(char *str, char *delimiter)
{
char **ptr_token;
char *token;
int i = 0;
ptr_token = malloc(sizeof(char *) * 1024);
token = _strtok(str, delimiter);
while (token)
{
ptr_token[i] = token;
token = strtok(NULL, delimiter);
i++;
}
ptr_token[i] = NULL;
return (ptr_token);
}