An efficient way to split a string into an array based on a character like% in C?

A similar thing can be found here.

I need to split a sentence into a char array based on the appearance of a character example:%

Example

If my offer is my% healthy% dog , then I should be able to get my, healthy and dog separately. It could be in a loop as well.

TX

+2


source to share


5 answers


strtok

is a standard C function that allows string tokenization.

#include <stdio.h>
#include <string.h>

int main()
{
        char c[] = "my%healthy%dog";
        char *token = strtok(c, "%");
        while (token != NULL)
        {
                printf("%s\n", token);
                token = strtok(NULL, "%");
        }
        return 0;
}


$ ./a.exe
my
healthy
dog

      



Also note that it strtok

uses static variables internally, so it is not thread safe. For thread safety, you will have to use a function strtok_r

.

+4


source


Check out the strtok function in string.h.



This is a good tutorial on how it works: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

0


source


The solution is strtok () in string.h.

Here's a good use case.

/* strtok example by mind@metalshell.com
 *
 * This is an example on string tokenizing
 *
 * 02/19/2002
 *
 * http://www.metalshell.com
 *
 */

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
        int x = 1;
        char str[]="this:is:a:test:of:string:tokenizing";
        char *str1;

        /* print what we have so far */
        printf("String: %s\n", str);

        /* extract first string from string sequence */
        str1 = strtok(str, ":");

        /* print first string after tokenized */
        printf("%i: %s\n", x, str1);

        /* loop until finishied */
        while (1)
        {
                /* extract string from string sequence */
                str1 = strtok(NULL, ":");

                /* check if there is nothing else to extract */
                if (str1 == NULL)
                {
                        printf("Tokenizing complete\n");
                        exit(0);
                }

                /* print string after tokenized */
                printf("%i: %s\n", x, str1);
                x++;
        }

        return 0;

}

      

What confuses people about strtok is the first time you call a method with the first argument you pass, a pointer to the string you want to tokenize, and subsequent calls that you pass in NULL. strtok uses a static variable in its implementation to keep track of where it should start searching with subsequent calls.

  • By passing NULL, you tell strtok to continue searching from where we left off last time.
  • Passing a pointer! = NULL you are telling the tokenizer that you are starting at the beginning of a new line, so ignore the previous state information.
0


source


using strtok.

Refer this :

0


source


strtok () should do the trick. You can also call it again using NULL as the first argument to search from where you left off.

From the documentation :
Each subsequent call with a null pointer as the value of the first argument starts the search at the stored pointer and behaves as described above.

0


source







All Articles