-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.c
More file actions
41 lines (31 loc) · 873 Bytes
/
tokenizer.c
File metadata and controls
41 lines (31 loc) · 873 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tokenizer.h"
#include "utils.h"
char** tokenize_input(const char* input) {
int max_tokens = 10;
char** tokens = (char**)malloc(max_tokens * sizeof(char*));
char* token = strtok((char*)input, " \t");
int token_count = 0;
while (token != NULL) {
tokens[token_count] = strdup(token);
token_count++;
if (token_count >= max_tokens) {
max_tokens += 10;
tokens = (char**)realloc(tokens, max_tokens * sizeof(char*));
}
token = strtok(NULL, " \t");
}
tokens[token_count] = NULL; // Set the last element to NULL
return tokens;
}
void free_tokens(char** tokens) {
if (tokens == NULL) {
return;
}
for (int i = 0; tokens[i] != NULL; i++) {
free(tokens[i]);
}
free(tokens);
}