-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_str.c
47 lines (42 loc) · 933 Bytes
/
split_str.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
#include "shell.h"
/**
* split_string - a fucntion that splits a string.
* @s: string.
* @len: line length
* @delim: delimiter
* Return: pointer to an array of array.
*/
char **split_string(char *s, int len, char delim)
{
char *word, **result = {NULL};
int r_size, w_count, in_word, w_len, start, end, i;
r_size = w_count = in_word = 0;
start = end = -1;
for (i = 0; i <= len; i++)
{
if (s[i] == delim || s[i] == '\0' || s[i] == '\t' || s[i] == '#')
{
if (in_word)
{
end = i;
w_len = end - start;
word = malloc(sizeof(char) * (w_len + 1));
_memcpy(word, s + start, w_len);
word[w_len] = '\0';
result = _realloc_array(result, ++r_size);
result[w_count] = word;
w_count++, in_word = 0;
}
}
else
{
if (!in_word)
start = i, in_word = 1;
}
if (s[i] == '#')
break;
}
result = _realloc_array(result, ++r_size);
result[w_count] = NULL;
return (result);
}