From b005e02bebc06bdd3e5af10df3e3b1d8c59b1402 Mon Sep 17 00:00:00 2001 From: Kofi-Awuah2020 Date: Wed, 6 Dec 2023 14:25:15 +0000 Subject: [PATCH] Tokenize an array --- tokenizer.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tokenizer.c diff --git a/tokenizer.c b/tokenizer.c new file mode 100644 index 0000000..2156611 --- /dev/null +++ b/tokenizer.c @@ -0,0 +1,34 @@ +#include "litshell.c" +/** + * tokenize - function returns an array of string based on given delimiter + * + * @str: string to be tokenized + * @delim: delimiter seperating the substrings + * + * Return: An array of strings + */ +char **tokenize(const char *str, const char *delim) +{ + static char *array_token[1024]; /* Declare an array to store substrings */ + int x = 0; /* Index of array of strings */ + char *temp_str; /* Save str in temp_str */ + + if (str != NULL) + { + temp_str = strdup(str); /* if str not null assign value to temp_str */ + } + + if (delim == NULL) + { + delim = " \t"; /* If delim is NULL assume space and tabs as delim */ + } + char *token = strtok(temp_str, delim); /* Store first token */ + + while (token != NULL) + { + array_token[x] = token; /* Store first token in index 0 of array */ + token = strtok(NULL, delim); + x++; /* Increment index of array of string */ + } + return (array_token); +}