简体   繁体   中英

How to split a string into an array of strings in C?

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?

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

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.  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM