-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.c
60 lines (51 loc) · 1.13 KB
/
tokens.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
#include "crikey.h"
/**
* append_token - Appends a string to the end of a linked list
* @head: Pointer to a pointer pointing to the first node in the linked list
* @str: Pointer to the first character of the string that should be assigned
* to the new node
*
* Return: Pointer to the newly added node
*/
token_t *append_token(token_t **head, char *str)
{
token_t *node = malloc(sizeof(token_t));
token_t *tmp = *head;
if (node == NULL)
return (NULL);
node->str = str;
node->next = NULL;
if (*head != NULL)
{
for (tmp = *head; tmp->next != NULL; tmp = tmp->next)
;
tmp->next = node;
}
else
*head = node;
return (node);
}
/**
* tokenize - Tokenizes a string into a linked list
* @head: Pointer to a pointer pointing to the first item in the linked list
* of tokens
* @input: The input string to be tokenized by the " " delimiter
*
* Return: Number of tokens tokenized
*/
int tokenize(token_t **head, char *input)
{
char *tmp = NULL;
int len = 0;
tmp = _strtok(input, ' ');
while (tmp != NULL)
{
if (*tmp != '\0')
{
len++;
append_token(head, tmp);
}
tmp = _strtok(NULL, ' ');
}
return (len);
}