I’ve started learning C, and coming from PHP, I hate the lack of explode() so I decided to create my own.
Here’s what I have so far:
#include <stdio.h>
#include <windows.h>
char * explode(char * toExplode, char * delimiter) /* Need to add a buffer size here */
{
char * token;
token = strtok(toExplode, delimiter);
token = strtok(NULL, delimiter);
return token;
}
int main(void)
{
char string[] = "This is a string yaaaaay";
char * exploded;
exploded = explode(string, " ");
printf("%s\n", exploded); /* Should currently return 'is' */
return 0;
}
So far, it’s working just as I expect it to. However, now I need to create a 2D array of variable size in the first dimension (actually, both dimensions.)
I was thinking of doing something like char * explode(char * toExplode, char * delimiter, int length = strlen(toExplode)) so that I could either specify the length or have it set it default. This of course doesn’t work though, and I have no idea where to go from here.
What can I try next?
If you’re stuck using C then the solution I’d recommend is rolling two functions; one with length, one without.
So, the latter just works out the length for you and passes it to the former and returns the result.