Create something like php's explode()
Hi again.
I'm trying to make something that will behave somewhat like php's explode(), but im failing to do so.
Here's what I got so far:
Code:
#include <stdio.h>
#include <string.h>
/*
This program is suppsed to take each word from 'string' and add it into
an array (word_array) so that each word can be accessed via word_array.
Like, word_array[1] will be 'random', word_array[2] will be 'stupid' and so on.
*/
int main()
{
char string[] = "random stupid text and other stuff, even a weird number like 124";
char word[64]; // will contain one word tempoarly (i.e 'random') before it's added into word_array
char *word_array[1024]; // will contain an array of words (i.e "random", "stupid")
int i = 0;
int a = 0;
int o = 0;
size_t x = strlen(string);
while( i < x )
{
if ( string[i] != ' ' )
{
word[o] = string[i];
o++;
}
else // We hit a ' ' (space), so that means we (should) have a word in our (word) array. let's add it to word_array;
{
printf("word = %s\n", word); // debug
word_array[a] = word;
/* This is where it's supposed to re-create the 'word'-array
delete word;
char word[64]; */
a++;
o = '0';
}
i++;
}
printf("%c\n", word_array);
}
Any suggestions/ideas/pointers?