Thread: Problem with string tokenizing

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    Problem with string tokenizing

    I'm trying to create a string tokenizing program. Here's what I got:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Gets the number of tokens from a given string and delimiters
    int getNumeroTokens(char* string,const char* delimitadores);
    
    //Gets tokens from a string, saving them under a char**
    char** stringTokenize(char* string,const char* delimitadores);
    
    //Clones a string
    char* stringClone(char* string);
    
    //Gets a free position on a given array
    int getPosicaoLivre(char** array,int array_length);
    
    int main() {    
        char* string = malloc(BUFSIZ*sizeof(char));
        printf("String > ");
        fgets(string,BUFSIZ,stdin);
        
        char* delimitadores = malloc(BUFSIZ*sizeof(char));
        printf("Delimitadores > ");
        fgets(delimitadores,BUFSIZ,stdin);
        
        int n = getNumeroTokens(string,delimitadores);
        char** tokens = stringTokenize(string,delimitadores);
        
        int i;
        for (i=0;i<n;i++) printf("%s\n",tokens[i]);
        
        system("pause");
        return 0;
    }
    
    int getNumeroTokens(char* string,const char* delimitadores) {
        int ret=0;
        char* temp = stringClone(string);
        char* token_actual = strtok(temp,delimitadores);
        while (token_actual != NULL) {
              ret++;
              token_actual = strtok(NULL,delimitadores);
        }
        return ret;
    }
    
    char** stringTokenize(char* string,const char* delimitadores) {
        int i;
        int n = getNumeroTokens(string,delimitadores);
        printf("Debug\n");
        char** ret = malloc(n*sizeof(char*));
        printf("Debug\n");
        for (i=0;i<n;i++) ret[i]=NULL;
        char* temp = stringClone(string);
        char* token_actual = strtok(temp,delimitadores);
        while (token_actual != NULL) {
              int pos = getPosicaoLivre(ret,n);
              ret[pos]=stringClone(token_actual);
              token_actual = strtok(NULL,delimitadores);
        }
        return ret;
    }
    
    char* stringClone(char* string) {
        char* ret = malloc(sizeof(string));
        strcpy(ret,string);
        return ret;
    }
    
    int getPosicaoLivre(char** array,int array_length) {
        int i;
        for (i=0;i<array_length;i++) if (array[i]==NULL) return i;
        return -1;
    }
    So, basically, my program asks the user for a string, and then for its delimiters. Then, I get the tokens on a char** using my own stringTokenize function and finally print the obtained tokens.

    Right. Now, if I compile my program, everything's OK. The problem is when I execute it.

    If I input:
    String > a-b
    Delimiters > -

    ...it prints the tokens "a" and "b". OK. That's what I want in that case.

    But if I input:
    String > bananas/apples
    Delimiters > /

    ...it fails here:

    Code:
    printf("Debug\n");
    char** ret = malloc(n*sizeof(char*)); //<--- HERE
    printf("Debug\n");
    Please tell me what I'm doing wrong. Thank you in advance.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    your stringClone() function is broken

    Code:
    char* stringClone(char* string) {
    //    char* ret = malloc(sizeof(string));        // sizeof(string) is typically just 4 bytes
        char* ret = malloc(strlen(string)+1);       // length of string plus one for the terminating '\0'
        strcpy(ret,string);
        return ret;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Wow. You're right. Thank you very much.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you have a lot of memory leaks in your program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Are you suggesting I call the free() function upon my pointers?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    it will be a good thing to do in apppropriate places

    (for example you can try to call you getNumeroTokens function in a very long loop and see what will be happaning with your program memory
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM