Repository navigation
Expand file tree
/
Copy path_strtok.c
More file actions
83 lines (80 loc) · 1.56 KB
/
Copy path_strtok.c
File metadata and controls
83 lines (80 loc) · 1.56 KB
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
#include "shell.h"
/**
* count_words - counts separate words in string
* @str: pointer to s
* @delim: delimiter
* Return: number of words
*/
int count_words(char *str, char delim)
{
int i, count;
i = 0;
count = 0;
while (str[i] != '\0')
{
if (str[i] != delim && (str[i + 1] == delim || str[i + 1] ==
'\t' || str[i + 1] == '\0'))
count++;
i++;
}
return (count);
}
/**
* _wrdlen - returns the lenght of a word
* @s: pointer to s
* @delim: delimiter
* Return: lenght
*/
int _wrdlen(char *s, char delim)
{
int c = 0; /* count */
while (*(s + c) != delim && *(s + c) != '\0' && *(s + c) != '\t')
c++;
return (c);
}
/**
* strtow - splits a string into words
* @str: string to break
* @delim: delimiter
* Return: array of strings(words)
*/
char **strtow(char *str, char delim)
{
int i, j, k, h, c, len;
char **words;
if (str == NULL || str[0] == '\0')
return (NULL);
c = count_words(str, delim);
if (c == 0)
return (NULL);
words = malloc(sizeof(char *) * (c + 1));
if (words == NULL)
return (NULL);
i = 0;
j = 0;
while (str[i] != '\0')
{
while ((str[i] == delim || str[i] == '\t') && str[i] != '\0')
i++;
if (str[i] == '\0')
{
words[j] = NULL;
return (words);
}
words[j] = malloc(sizeof(char) * (_wrdlen(str + i, delim) + 1));
if (words[j] == NULL)
{
for (k = j - 1; k >= 0; k--)
free(words[k]);
free(words);
return (NULL);
}
len = _wrdlen(str + i, delim);
for (h = 0; h < len && str[i] != '\0'; h++, i++)
words[j][h] = str[i];
words[j][h] = '\0';
j++;
}
words[j] = NULL;
return (words);
}