Home > Back-end >  How to split a string into an array of strings in C?
How to split a string into an array of strings in C?

Time:06-02

I've been having this issue for quite a long time so I wanted to ask how to split a string into an array of "Words"?

I've tried strtok and strtok_r but it doesn't seem to work the way I intend it to:

    char str[] = "This is a sentence.";

    // Returns first token
    char* token = strtok(str, " ");
    char *tokens[500];
  
    // Keep printing tokens while one of the
    // delimiters present in str[].
    
    
    for (int i = 0; i < 3; i  ) {
        while (token != NULL) {
            token = strtok(NULL, " ");
            strcpy(tokens[i], token);
        }
        printf("%s\n", tokens[i]);
    }
    
    return 0;

This doesn't print out anything, can anyone help?

CodePudding user response:

If you want to split str in tokens and store the tokens in buffer tokens, here's how you can do it (please have a look at the comments I added):

char str[] = "This is a sentence.";

// Initialize tokens to NULL
char* tokens[500] = {0};

// Split the string in tokens and count the tokens:
size_t tokenCount = 0;
static size_t const max_token_count = sizeof(tokens) / sizeof(tokens[0]);
for (char* token = strtok(str, " ");
     token != NULL                  &&   tokenCount != max_token_count;
//   ^ Loop until there are tokens  and  ^ buffer not overflown
     token = strtok(NULL, " ")) {

    tokens[tokenCount  ] = token;
    //                   ^ No need to allocate memory here, if you
    //                     are going to use tokens before str goes
    //                     out of scope.

}

// You may handle the case when there are more tokens in str that
// couldn't fit into buffer tokens here

for (size_t i = 0; i != tokenCount;   i)
    puts(tokens[i]);

Output

This
is
a
sentence.

Please note that, at the end of the snippet above, str will be modified: the spaces will be replaced by character '\0':

char str[] = "This is a sentence.";
//                ^  ^ ^

So, if you

puts(str);

you'll only get

This

CodePudding user response:

here is a possible help if I understand correctly what your needs are. I use tabs and remove all the calls for strtok.

From this code you will print only the first word, I let you find the way to print all of them :)

int main(){
    char str[] = "This is a sentence.";

    // Returns first token
    char tokens[500];
  
    // Keep printing tokens while one of the
    // delimiters present in str[].
    
    
    for (int i = 0; i < sizeof(tokens); i  ) {
        if (str[i] == ' '){
            break;
        }
        tokens[i] = str[i];
    }
    printf("%s\n", tokens);

    
    return 0;
}

EDIT

full solution:

int main(){
    char str[] = "This is a sentence.";
    char tokens[500];
    memset(tokens,'\0', sizeof(tokens));

        for(int i=0, j=0; i< sizeof(tokens); i  )
        {

            if (str[i] == ' ' || str[i] == '\n' || str[i] == 0) {
                printf("%s\n", tokens);
                //reinit buffer token
                memset(tokens,'\0', sizeof(tokens));
                j=0;            
            }else{
                tokens[j] = str[i];
                j  ;
            }
            if (str[i] == 0) {
                break;              //end of string found, exit the loop
            }

        }
    return 0;
}

Output

This  
is  
a  
sentence.  
  • Related