how to parse a string

This is a discussion on how to parse a string within the C Programming forums, part of the General Programming Boards category; You have no reason to think that it's better but ok. Regardless, you're not supposed to bump threads more than ...

  1. #46
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    You have no reason to think that it's better but ok. Regardless, you're not supposed to bump threads more than two weeks old, it's against the rules. We're approaching the end of May.

  2. #47
    Registered User
    Join Date
    May 2008
    Posts
    5
    My vast apologies for the error. The thread was rather near the top of the list while looking at the New Posts. Quickly scanning the date of the post prior to mine shows a date of 4/20--I must have had a thinking error and interpreted it as 5/20, or yesterday.

    As to your first comment: yes I do.

  3. #48
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    The only thing your solution does differently than strtok is that it stores the result in a different place. That solves only one of strtok's problems, and not very well. If the space passed is too short, your function simply barfs, which to me is not a solution at all. I can't like that decision.

    One of the most legitimate concerns with strtok is that it screws up empty tokens.
    Code:
    int main ( void )
    {
        const char * parseme = "test;test2;;test3\n";
        char part[BUFSIZ];
        const char * result = NULL;
        while ( ( result = better_strtok( parseme, ";\n", part, sizeof part ) ) != NULL ) {
            parseme = result;
            printf( "-> \"%s\"\n", part );
        }
        return 0;
    }
    -> "test"
    -> "test2"
    -> "test3"

    That's a pretty bad result too. When I can copy the string and pass the copy to strtok with the same results, I would prefer that to your broken function.

  4. #49
    Registered User
    Join Date
    May 2008
    Posts
    5
    I trust that despite rules to the contrary that it's OK to post here.

    To address your first concern, it's a simple matter to malloc the buffer to hold the result. The function result now indicates whether or not an entry was returned rather than a pointer to the memory. This was largely done to be compatible with the version of the code that follows this one.

    Code:
    int better_strtok(char **str, char *delim, char **result) {
    	char *start = *str;
    	while (*start && strchr(delim, *start)) start++;
    	if (*start == '\0') return 0;
    	char *ptr = start;
    	while (1) {
    		if (strchr(delim, *ptr)) {
    			*result = malloc(ptr - start + 1);
    			strncpy(*result, start, ptr - start);
    			(*result)[ptr - start] = '\0';
    			*str = ptr;
    			return 1;
    		}
    		ptr++;
    	}
    }
    
    int main(void) {	
    	char *str = "test;test2;;test3\n";
    	char *buf;
    	char *ptr = str;
    	while (better_strtok(&ptr, ";\n", &buf)) {
    		printf("> %s\n", buf);
    		free(buf);
    	}
    }
    
    > test
    > test2
    > test3
    To address your second concern, which really changes this from being a better strtok to being an entirely different beast, it's a matter of not treating multiple delimiters as a single entity. Potential issues here are what to do if the first character is a deliminter and/or the last character is a delimiter: do these genuinely delimit entries or are they merely there for completeness? The code below treats a delimiter as actually having an entry of interest on either side of it.

    Code:
    int better_strtok(char **str, char *delim, char **result) {
    	char *start = *str;
    	char *ptr = start;
    	while (1) {
    		if (strchr(delim, *ptr)) {
    			*result = malloc(ptr - start + 1);
    			strncpy(*result, start, ptr - start);
    			(*result)[ptr - start] = '\0';
    			*str = ptr + 1;
    			return *ptr;
    		}
    		ptr++;
    	}
    }
    
    int main(void) {	
    	char *str = "test;test2;;test3\n";
    	ptr = str;
    	int result = 1;
    	while (result) {
    		result = better_strtok(&ptr, ";\n", &buf);
    		printf("-> %s\n", buf);
    		free(buf);
    	}
    }
    
    > test
    > test2
    >
    > test3
    >

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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