How to tokenize a string containing null values ​​using strtok_r

I have a string containing some comma separated values. The value may or may not be NULL. eg:

strcpy(result, "Hello,world,,,wow,");

      

I want null values ​​to be printed as well. How can I proceed using strtok_r which also gives me NULL values.

I've tried this:

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

int main(void) {

    char result[512] = { 0 };
    strcpy(result, "Hello,world,,,wow");
    char *token, *endToken;
    int i = 0;
    token = strtok(result, ",");
    while (i < 5) {
        printf("%d\n", ++i);
        printf("%s\n", token);
        token = strtok(NULL, ",");
    }
    return 0;
}

      

and the output is:

1
Hello
2
world
3
wow
4
Segmentation fault (core dumped)

      

I know why it is giving segmentation fault. I want the solution to turn out like this:

1
Hello
2
World
3
*
4
*
5
wow

      

I want it to *

print for null tokens, but null tokens are not even retrieved.

+3


source to share


3 answers


From the strtok_r man page:

A sequence of two or more adjacent delimiter characters in the parsed string is considered the only delimiter.



So this won't work in your case. But you can use code like this:

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

int main(void) {
    int i = 0;
    char result[512];
    char *str = result, *ptr;
    strcpy(result, "Hello,world,,,wow");
    while (1) {
        ptr = strchr(str, ',');
        if (ptr != NULL) {
            *ptr = 0;
        }
        printf("%d\n", ++i);
        printf("%s\n", str);
        if (ptr == NULL) {
            break;
        }
        str = ptr + 1;
    }
    return 0;
}

      

+2


source


If you haven't strsep()

, you can flip yours .

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

int main(void) {

    char result[512] = "Hello,world,,,wow";
    char *token, *endToken;
    int i = 0;

    token = result;
    do {
        endToken = strchr(token, ',');
        if (endToken)
            *endToken = '\0';           // terminate token
        printf("%d\n", ++i);
        if (*token == '\0')             // substitute the empty string
            printf("*\n");
        else
            printf("%s\n", token);
        if (endToken)
            token = endToken + 1;
    } while (endToken);
    return 0;
}

      



Program output:

1
Hello
2
world
3
*
4
*
5
wow

      

+1


source


For strtok to search for a token, there must be the first character that is not a delimiter. It only returns NULL when it reaches the end of the string, i.e. When he finds a symbol '\0'

.

To determine the start and end of the token, the function first scans from the start position for the first character not contained in the delimiters (which becomes the start of the token) . And then the scan starts at that start of the token for the first character contained in the delimiters, which becomes the end of the token. The scan also stops if a terminating null character is found.

http://www.cplusplus.com/reference/cstring/strtok

0


source







All Articles