Split a char array by delimiter

I have a char array like "abc:def:ghi#jkl:mno:pqr#stu:vxz:wyk" which I want to split firstly by "#", then each of the resulted char array by ":". So I have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
char* largechars = "def:def:def#abc:abc:abc#ghi:ghi:ghi";
char* chars_array = strtok(largechars, "#");

while (chars_array != NULL) {
    char* subchar_array = strtok(chars_array, ":");
          
    while (subchar_array != NULL) {
        MessageBox(NULL, subchar_array, NULL, NULL);
        subchar_array = strtok(NULL, ":");
    }
    
    chars_array = strtok (NULL, "#");
}


With this code only three messageboxes shows up, with, 'abc', 'def' and 'ghi'. What I want is that after splitting the largechars by "#" into chars_array (which would containt three elements), those three resulted elements should be splitted each one by ":" into subchar_array (which would contain three elements). If I keep only:

1
2
3
4
5
6
7
char* largechars = "def:def:def#abc:abc:abc#ghi:ghi:ghi";
char* chars_array = strtok(largechars, "#");

while (chars_array != NULL) {
    MessageBox(NULL, chars_array, NULL, NULL);
    chars_array = strtok (NULL, "#");
}


'def:def:def', then 'abc:abc:abc', then 'ghi:ghi:ghi' is shown by MessageBox but when I try to split each of them then only the first one is splitted. What do you recommend? Thanks!
First, you're modifying a read-only string, your first line of code must be
char largechars[] = "def:def:def#abc:abc:abc#ghi:ghi:ghi";

I recommend using C++ language facilities (stringstream and getline, for example), or, to make it completely trivial, the boost library, but if you must do it with strtok, you will need to tell strtok about the beginning of each of the three subarrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>
#include <vector>
int main()
{
    char largechars[] = "def1:def2:def3#abc1:abc2:abc3#ghi1:ghi2:ghi3";
    std::vector<char*> v;
    char* chars_array = strtok(largechars, "#");
    while(chars_array)
    {
        v.push_back(chars_array);
        chars_array = strtok(NULL, "#");
    }

    for(size_t n = 0; n < v.size(); ++n)
    {
        char* subchar_array = strtok(v[n], ":");
        while (subchar_array) {
//            MessageBox(NULL, subchar_array, NULL, NULL);
            std::cout << subchar_array << '\n';
            subchar_array = strtok(NULL, ":");
        }
    }
}

demo: http://ideone.com/n18Tu

Or just tell strtok to use both delimiters

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstring>
int main()
{
    char largechars[] = "def1:def2:def3#abc1:abc2:abc3#ghi1:ghi2:ghi3";
    char* chars_array = strtok(largechars, "#:");
    while(chars_array)
    {
//        MessageBox(NULL, subchar_array, NULL, NULL);
        std::cout << chars_array << '\n';
        chars_array = strtok(NULL, "#:");
    }
}

demo: http://ideone.com/PC2A7

It works! For the moment I will remain with the strtok. Thank you!
Topic archived. No new replies allowed.