Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:01:55+00:00 2026-06-12T15:01:55+00:00

Possible Duplicate: Split string with delimiters in C I’m searching a good way to

  • 0

Possible Duplicate:
Split string with delimiters in C

I’m searching a good way to “explode” a char* into other char* using a delimiter.

My delimiter will be #

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:01:56+00:00Added an answer on June 12, 2026 at 3:01 pm

    You can use strtok like CrazyCasta said but his/hers code is wrong.

    char *tok;
    char *src = malloc(strlen(srcStr) + 1);
    memcpy(src, srcStr);
    
    tok = strtok(src, "#");
    if(tok == NULL)
    {
        printf("no tokens found");
        free(src);
        return ???;
    }
    printf("%s ; ", tok);
    while((tok = strtok(NULL, "#")))
        printf("%s ; ", tok);
    printf("\n");
    free(str);
    

    Be aware that strtok has to be called the first time with the source pointer, after that you have to use NULL. Also src must be writeable because strtok writes \0 to terminate the found strings. Hence, depending on how you read the string (and whether you are going to use it afterwards or not), you should do a copy of it. But as I said, this is not always necessary.

    EDIT:

    an explode function could look like this:

    char *strdup(const char *src)
    {
        char *tmp = malloc(strlen(src) + 1);
        if(tmp)
            strcpy(tmp, src);
        return tmp;
    }
    
    void explode(const char *src, const char *tokens, char ***list, size_t *len)
    {   
        if(src == NULL || list == NULL || len == NULL)
            return;
    
        char *str, *copy, **_list = NULL, **tmp;
        *list = NULL;
        *len  = 0;
    
        copy = strdup(src);
        if(copy == NULL)
            return;
    
        str = strtok(copy, tokens);
        if(str == NULL)
            goto free_and_exit;
    
        _list = realloc(NULL, sizeof *_list);
        if(_list == NULL)
            goto free_and_exit;
    
        _list[*len] = strdup(str);
        if(_list[*len] == NULL)
            goto free_and_exit;
        (*len)++;
    
    
        while((str = strtok(NULL, tokens)))
        {   
            tmp = realloc(_list, (sizeof *_list) * (*len + 1));
            if(tmp == NULL)
                goto free_and_exit;
    
            _list = tmp;
    
            _list[*len] = strdup(str);
            if(_list[*len] == NULL)
                goto free_and_exit;
            (*len)++;
        }
    
    
    free_and_exit:
        *list = _list;
        free(copy);
    }
    

    then you have to call it:

    char **list;
    size_t i, len;
    explode("this;is;a;string", ";", &list, &len);
    for(i = 0; i < len; ++i)
        printf("%d: %s\n", i+1, list[i]);
    
    /* free list */
    for(i = 0; i < len; ++i)
        free(list[i]);
    free(list);
    

    this is an example running with valgrind:

    valgrind ./a 
    ==18675== Memcheck, a memory error detector
    ==18675== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. 
    ==18675== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
    ==18675== Command: ./a 
    ==18675== 
    1: this
    2: is
    3: a
    4: string
    ==18675== 
    ==18675== HEAP SUMMARY:
    ==18675==     in use at exit: 0 bytes in 0 blocks
    ==18675==   total heap usage: 9 allocs, 9 frees, 114 bytes allocated
    ==18675== 
    ==18675== All heap blocks were freed -- no leaks are possible
    ==18675== 
    ==18675== For counts of detected and suppressed errors, rerun with: -v
    ==18675== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Split string with delimiters in C What's the best way to split
Possible Duplicate: How can I split a comma delimited string into an array in
Possible Duplicate: How can I split a comma delimited string into an array in
Possible Duplicate: Split String into smaller Strings by length variable I have seen solutions
Possible Duplicate: How to split string into array as integers I have a 3x2
Possible Duplicate: Split string by delimiter, but not if it is escaped I have
Possible Duplicate: Split string based on delimiter in bash? i have a shell script
Possible Duplicate: How do i split a String into multiple values? I have a
Possible Duplicate: string split in c# How to split Name string into First Name
Possible Duplicate: Python: Split string with multiple delimiters I have a program where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.