jabk
jabk

Reputation: 1468

How to store tokens(strtok) in a pointer on an array

I still have some problems and I think if I manage to figure this one out that I will finally get the grip on it.

I have this line that I strtok it by the delimiter space. Now I want to store all tokens in a pointer on the array char* tokens[50]. How would I store all the tokens in this pointer and how would I access all tokens once they are stored. I think I'd also need a counter int token_count.

Upvotes: 1

Views: 9773

Answers (2)

vladimirm
vladimirm

Reputation: 261

Why don't you use regular expressions in C++11? You can represent a space (one or more) as a simple regular expression "\s+" and use a regex token iterator to iterate through the tokens, you can store the tokens in a vector from there.. Here is an example that just prints out the tokens.

#include <regex>
#include <string>
#include <iostream>
using namespace std;


int main()
{

    regex re("\\s+");
    string s = "Token1 token2   token3"; //example string

    sregex_token_iterator reg_end;
    for(sregex_token_iterator it(s.begin(), s.end(), re, -1); it != reg_end; ++it) {
         cout << it->str() << endl;
    }
}

Upvotes: -2

Kerrek SB
Kerrek SB

Reputation: 477100

This is straight-forward. For example:

char * tokens[50];
size_t n = 0;

for (char * p = strtok(line, " "); p; p = strtok(NULL, " "))
{
    if (n >= 50)
    {
        // maximum number of storable tokens exceeded
        break;
    }
    tokens[n++] = p;
}

for (size_t i = 0; i != n; ++i)
{
     printf("Token %zu is '%s'.\n", i, tokens[i]);
}

Note that line must point to a mutable character string, since strtok mangles the string.

Upvotes: 4

Related Questions