split string function

This is a discussion on split string function within the C Programming forums, part of the General Programming Boards category; hello. i am trying to make a split function that takes in a string and a delimiter, and returns a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    split string function

    hello. i am trying to make a split function that takes in a string and a delimiter, and returns a array of strings that have been separated by the delimiter.

    here is my code so far:
    Code:
    char  * split(char string[], char * sep){
    	// char str[] = &string;
    	char * pch;
    	char * out[3];
    	int i=0;
    	printf ("h1\n");
    	pch = strtok (string," ,");
    	printf ("h2\n");
    	while (pch != NULL)
    	{
    		printf ("\"%s\"\n",pch);
    		out[i] = pch;
    		i++;
    		if(pch != NULL) pch = strtok (NULL, " ,");
    	}
    	//out[i] = '\0';
    	printf("h3 %s\n",out[0]);
    	return out;
    }
    
    int main(){
    	
    	printf("start\n");
    	char * out = split(str, " ,");
    	printf("end\n");
    	printf ("out 1: %s\n",out[0]); // segmentation fault happens here
    	printf ("out 2: %s\n",out[1]);
    	printf ("out 3: %s\n",out[2]);
    	return 0;
    }
    the input i used is: "this, is, crazy"

    i get a segmentation fault after the printf of "end"

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    listen to your compilers warnings.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    something to the effect of this:
    Code:
    database.c: In function ‘split’:
    database.c:47: warning: return from incompatible pointer type
    database.c:47: warning: function returns address of local variable
    database.c: In function ‘test’:
    database.c:70: error: incompatible types in assignment
    database.c: In function ‘start_database’:
    database.c:87: warning: excess elements in scalar initializer
    database.c:87: warning: (near initialization for ‘row’)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    is this file named 'database.c'? if so then post the full code.
    it would be helpful to us if you commented or somehow let us know what lines are #47, 70, and 87.

    edit: it looks like your trying to do something with the variable 'out'. however it isnt initialized.
    Code:
    char * out[3];
    is a pointer to an array of three chars.
    Last edited by nadroj; 04-21-2007 at 11:44 AM.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's not that simple what you are trying to do. Right now I don't have the nerve to try to explain how to do that.
    try this code.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    char  ** split(char string[], int * num, char * sep) {
    	char * pch;
    	char ** out = 0;
            int i = 0;
    	pch = strtok (string, sep );
           
    	while (pch != 0 ) {
                    out = realloc(out, (  i + 1 ) * sizeof( char * ));
    		out[i] = malloc( strlen(pch ) + 1 );
                    strcpy( out[i], pch );
                    ++i;
    		pch = strtok (NULL, sep);
    	}
            *num = i;
    	return out;
    }
    
    
    int main() {
        char str[255] = "one, two, tree, four,five  six";
        int num = 0;
        int i = 0;
        char ** tokens = split( str, &num, " ,");
        for( i = 0; i < num; ++i )
           printf("&#37;s\n", tokens[i] );
    
        for( i = 0; i < num; ++i )
           free( tokens[i] );
    
        free(tokens); 
    }
    Feel free to ask how it works.
    Kurt

    EDIT: proper error checking needs to be added.
    Last edited by ZuK; 04-21-2007 at 12:48 PM.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    thank you ZuK this works.
    i love the versitilty of c.. but i have worked with python for so long, i forgot to appreciate c.

    i would like to know how this works..
    like the functions
    realloc and free

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21