Thread: Create something like php's explode()

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    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?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1.You never allocate any space for the strings in the word_array.
    2. you have to use strcpy to copy word into the word_array.
    Kurt

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    printf("word = %s\n", word); // debug
    You never null terminate word, so this will overflow.

    Code:
    word_array[a] = word;
    Nope, this wont work. You overwrite the word array for every word, so you basically have every index in word_array pointing to the same exact thing.

    You need to do something like this:
    Code:
    word_array[a] = malloc(strlen(word) + 1);
    strcpy(word_array[a],word);
    Code:
    delete word;
    First of all, delete is a C++ keyword, not C. Second of all, it only works on dynamically allocated memory which word is not.

    Code:
    o = '0';
    Not sure why this is here, but take it out as it does nothing.

    Code:
    printf("%c\n", word_array);
    No. First of all, to print a string, use %s, not %c. Second of all, word_array is NOT a string, it is an array of strings. More appropriate would be:
    Code:
    printf("%s\n", word_array[0]);

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Alright, thanks guys.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And one other thing:
    Code:
    int i;
    size_t x;
    
    while( i < x )
    On the last line, with full compiler warnings, you might get something like "warning: signed/unsigned type mismatch". To fix that, cast it:
    Code:
    while( (size_t) i < x )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    better yet, fix the types to be compatible to start with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 11:32 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  4. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM