Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tokenizer.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take note of the following text:

static char *array_token[1024];

Upon inspection of the aforementioned line, I noticed that it would be better to allocate only the necessary amount of memory for tokenizing strings. Therefore, please adjust your solution to determine the precise amount of memory needed for the string that requires tokenizing.

Additionally, I would like to know if you have tested your function and what your findings were. I noticed a few bugs and I'm interested to know if you can confirm that your solution works.

Furthermore, I observed that your solution lacks memory deallocation for the temporary variable used. Also, you do not allocate memory for each token. As a result, your solution requires further work. However, I am willing to see how it performs based on your testing.

Original file line number Diff line number Diff line change
@@ -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);
}