Thread: Extracting info from string

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Extracting info from string

    I have a string containing '|' delimiters which is like :

    Testing|1|2|3|4

    What I want to do is to extract the data in between the delimiters respectively.
    The problem is i do not know how many '|' are in between...

    and assuming i do not know how many delimiters there are, how do i detect the number of '|' or does anyone know a way around this?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    You can use strtok() for the same. Look up the man pages.

    Anoop.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    strtok is okay as long as you don't have to worry about concurrency or nested calls. In those cases strtok will fail miserably. Some libraries have a strtok_r that is reentrant, but I've found that it's easier just to follow in the footsteps of argc and argv:
    Code:
    /* Warning: Ugly code follows. Sorry, written in haste. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int makeargv(char *plist, char ***argv, char *delim)
    {
        char   *pos = plist;
        size_t dlen = strlen(delim);
        int    argc = 0;
        int    i;
    
        /* Count args */
        while ((pos = strstr(pos, delim)) != NULL) {
            argc++;
            pos += dlen;
        }
    
        if ((*argv = malloc((argc + 1) * sizeof **argv)) == NULL)
            return -1;
    
        for (i = 0, pos = plist; i < argc; i++, plist = pos + dlen) {
            pos = strstr(plist, delim);
    
            if (((*argv)[i] = calloc((pos - plist) + 1, sizeof (*argv)[i])) == NULL) {
                while (--i >= 0)
                    free((*argv)[i]);
                free(*argv);
    
                return -1;
            }
    
            strncpy((*argv)[i], plist, pos - plist);
        }
    
        /* Last token */
        if (((*argv)[i] = calloc(strlen(plist) + 1, sizeof (*argv)[i])) == NULL) {
            while (--i >= 0)
                free((*argv)[i]);
            free(*argv);
    
            return -1;
        }
    
        strcpy((*argv)[i++], plist);
        (*argv)[i] = NULL;
    
        return argc + 1;
    }
    
    int main(void)
    {
        char **argv;
        char test[] = "Testing|1|2|3|4";
        int  argc;
        int  i;
    
        /* Using makeargv */
        if ((argc = makeargv(test, &argv, "|")) != -1) {
            for (i = 0; i < argc; i++) {
                puts(argv[i]);
                free(argv[i]);
            }
    
            free(argv);
        }
    
        return 0;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    oops, entered wrong board, message deleted. Sorry.
    Last edited by elad; 08-25-2003 at 09:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM