I want to split a string into token depending on different delimiters.

e.g. char * temp = "asfs:ggd,oper:eger,ropptujnsdn:hfhgw";
and firstly I want to split data by using "," (lets call a block). and then each block should again split using ":" and save each token's value. then finally I want to store each block(after splitting data inside the block) in a vector. This is the code i wrote 4 that but its not working...........

char * a, *b, *c, *temp;
  char str[] ="This,isasample,string";
  char * pch;
  
  pch = strtok (str, ",");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
	if(!(a = strtok (pch, ":")) ||
		!(b = strtok (NULL, ":"))||
		!(c = strtok (NULL, ":")))
		printf ("%s\n","error");
    //else
		pch = strtok (NULL, ",");
  }

I just happened to have posted in a thread very similar to this.
http://www.daniweb.com/forums/thread107523.html

Heres a few functions that may help.

#include<iostream>
using namespace std;

inline char *SubStr(char *text, int beg, int end) {
	register unsigned len = end - beg;
	register char *cut = new char[len];
	memcpy_s(cut,(rsize_t)len,&text[beg],(size_t)len);
	cut[len] = '\0';
	return cut;
}

inline bool TextContains(char *txt, char ch) {
	while (*txt) if (*txt++ == ch) return 1;
	return 0;
}

int CountWords(char *text, char *gaps) {
	bool t = 0, ot = 0;
	int wc = 0;
	while (TextContains(gaps, *text++));
	while (*text) {
		t = TextContains(gaps, *text++);
		if (t != ot) wc++;
		ot = t;
	}
	return (wc/2)+1;
}

char *GetWord(char *text, char *gaps, int bzIndex) {
	register unsigned int i = 0, sc = 0, ec = 0, g = 0;
	for (;text[i] && TextContains(gaps, text[i]);) i++;
	for (sc = i; text[i]; i++) {
		if (TextContains(gaps, text[i])) {
			while (text[i] && TextContains(gaps, text[i+1])) i++;
			if (++g == bzIndex) sc = i + 1;
		} else if (g == bzIndex) {
			while (text[i] && !TextContains(gaps, text[i])) i++;
			ec = i;
			break;
		}
	}
	return SubStr(text, sc, ec);
}

char **GetAllWords(char *text, char *gaps) {
	unsigned int NumWords = CountWords(text, gaps);
	char **words = new char*[NumWords];
	register unsigned int sc = 0, ec = 0, g = 0;
	for (unsigned int i = 0; g < NumWords; i++) {
		while (text[i] && TextContains(gaps, text[i])) i++;
		sc = i;
		while (text[i] && !TextContains(gaps, text[i])) i++;
		ec = i;
		words[g++] = SubStr(text, sc, ec);
	}
	return words;
}

int main() {
	char str[] = "Hello world";
	char **words = GetAllWords(str, " ");
	cout << words[0];
	cin.ignore();
	return 0;
}

Opps, I just realized I posted this in the C forum ;) I thought I was in C++.

Opps, I just realized I posted this in the C forum ;) I thought I was in C++.

You posted in the right place. It doesn't matter what compiler you use, its the code not the compiler that determines which board to post on.

[edit]Oops! I see the cout now. Oh well, not that hard to substitute printf()

Hi,

Please see the example below which does exactly the same you wanted.

main(int argc, char *argv[])
{
    char *str1, *str2, *token, *subtoken;
    char *saveptr1, *saveptr2;
    int j;
    if (argc != 4) {
        fprintf(stderr, "Usage: %s string delim subdelim\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }
    for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
        token = strtok_r(str1, argv[2], &saveptr1);
        if (token == NULL)
            break;
        printf("%d: %s0, j, token);
        for (str2 = token; ; str2 = NULL) {
            subtoken = strtok_r(str2, argv[3], &saveptr2);
            if (subtoken == NULL)
                break;
            printf("t --> %s0, subtoken);
        }
    }
    exit(EXIT_SUCCESS);
} /* main */

Hope this helps.

--saandeep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.