Strtoking where a delimiter appears after itself.

This is a discussion on Strtoking where a delimiter appears after itself. within the C Programming forums, part of the General Programming Boards category; Say I'm strtoking a string that is in the format a:b:c:d:e:f:g And I want a,b,c,d,e,f and g to go into ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    19

    Strtoking where a delimiter appears after itself.

    Say I'm strtoking a string that is in the format

    a:b:c:d:e:f:g

    And I want a,b,c,d,e,f and g to go into arrays made for them

    i.e.
    array[0] would contain a
    array[1] would contain b
    array[2] would contain c and so fourth

    I have the current code to do this:


    Code:
    int i = 1;
    int n;
    char *token;
    
    token = strtok(input,":"); 
    strcpy(Array[0],token);
    
    while ((token = strtok(NULL,":")) != NULL) 
    {
    	strcpy(Array[i] , token);
    	i++; 
    }
    However, when the string comes in the format a:b::d:e:f:g, it will put d in array[2] and therefore everything after b would be in the wrong array.

    How could I fix this?

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If "i" doesn't increment, that's the nature of strtok and I don't see much you can do about it other that writing your own function, which shouldn't be too hard.

    Since you were using strtok, it doesn't matter if you destroy the original string in the process, so something like this:
    Code:
    #include <stdio.h>
    
    int main() {
    	char string[]="a:b::c:d",
    		*array[10];  /* POINTERS */
    	int count=0, index=1;
    
    	array[0]=string;
    	while (string[count]!='\0') {
    		if (string[count]==':') {
    			array[index]=&string[count+1];
    			index++;
    			string[count]='\0';
    		}
    		count++;
    	}
    	
    	/* now verify */	
    	for (count=0; count<index; count++) { 
    		printf("element #%d: \"%s\"\n",count,array[count]); }
    	return 0;
    }
    
    Output:
    element #0: "a"
    element #1: "b"
    element #2: ""
    element #3: "c"
    element #4: "d"
    Even though the original string is chopped up in this, you need to keep it since "array" is only pointers (or you could use strcpy). Etc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    I copied your code into a test program just to see what happened and it seg faulted...

    I tracked it down to the while loop test but can't seem to fix it :-\

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    Oh, nevermind, I fixed it.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    Hmm, I've had a shot but with no success -

    Is there a way to convert your way of separating strings into something with a 2D array, as I need to reference specific characters in the string later in the code (i.e. Array[3][2] = a with the string "fishes:are:really:good:for:you")

    With your example, I tried to edit it to accommodate this but... it failed miserably.

    Working on the assumption that that there will be no more than 10 words in between the delimiter and no more than 10 characters in each word.

    Code:
    char string[] = "fishes:are:really:good:for:you";
    char array[10][10];
    
    *array[0] = *string;
    while (string[count] != '\0') 
    {
    	if (string[count]==':') 
    	{
    		strcpy(array[index], &string[count+1]);
    		index++;
    		string[count]='\0';
    	}
    	count++;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    row =  col = 0;
    
    while( s && (words[ row ][ col ] = *s) )
        if( *s == DELIMITER )
        {
            words[ row ][ col ] = '\0';
            row++;
            col = 0;
        }
        else
        {
            col++;
        }
    }
    That looks about right.

    Quzah.
    Last edited by quzah; 05-02-2009 at 06:16 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    Sorry...i don't quite understand what's going on there :-\

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while the pointer is not null, and while the character i'm assigning to the other array isn't the null character
        if the character being pointed at is the delimiter
            set this row's last character to a null character
            increment the row count
            reset the column count
        else
            increment the column count
    There ya go.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    Gotchya, cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the console always appears......
    By gandalf_bar in forum C++ Programming
    Replies: 7
    Last Post: 01-10-2005, 10:16 AM
  2. Character that appears max times in string
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-15-2004, 12:06 PM
  3. Socket Select() Delimiter
    By Lee A O in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2002, 06:05 PM
  4. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 05:27 PM
  5. scanf delimiter problem
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 01-18-2002, 11:15 AM

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