Split function

This is a discussion on Split function within the C Programming forums, part of the General Programming Boards category; is there any readily available split function in C, to split a string up when a delimiter is found? my ...

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

    Split function

    is there any readily available split function in C, to split a string up when a delimiter is found? my delimiter is not just one character, but 2 characters, is it possible in C?
    Only by the cross are you saved...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    yeah
    but if i'm not mistakened, strstr onli returns the location, but how do i actually split it up?
    Only by the cross are you saved...

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >but how do i actually split it up?

    strcpy/strncpy?

    Can you provide an example of what you are trying to do? For example, I want to take "Hello--world" and put "Hello" in one string and "world" in another.

    There are other string functions in the standard library that may be better suited to the task. But a better description will result in better replies.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I think the answer is in the Programming FAQ.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Do a search for strtok. That should help.
    The keyboard is the standard device used to cause computer errors!

  7. #7
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Is this what you're looking for?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *sbreak(char *s, char *tok, char *del)
    {
        char *new_s;
        char *p = tok;
        
        /* If no more string, bail */
        if (*s == '\0')
            return 0;
        
        /* If no delimiters, seek to end */
        if ((new_s = strstr(s, del)) == 0)
            new_s = s + strlen(s);
        
        while (s != new_s)
            *p++ = *s++;
        
        *p = '\0';
        
        /* Bump past the delimiter if it's there */
        if (*new_s)
            s += strlen(del);
        
        return s;
    }
    
    int main(void)
    {
        char  a[10];
        char  s[] = "this::is::a::::test";
        char *p = s;
        
        while ((p = sbreak(p, a, "::")))
        {
            if (*a)
                printf("%s\n", a);
        }
        
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    thanx ppl, will look thru wat u said...

    oh, wat i want to do is actually once i have a string, like

    hello||test

    i want to detect the delimiter which is "||" and then split and assign hello and test to different variables...
    Only by the cross are you saved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM

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