I want to split data in a string. The string will consist of several data. Before every element the length ofthe element will come. I want to exract data one by one.

e.g. char * abc = "[length ]acsfdnhsjg[len]gvsdgdvheg[len]efgwetrge";

can someone help me!!!!!!!

So it'll say like 13 and you want to grab the next 13 characters? Will the length actually be wrapped in []? If ya have boost installed use boost::regex for the simplest way. You might be able to use strtok to split them all with respect to [, skip till ] and grab the rest of the string. If [ and ] mark the size of the string you don't actually need the length.

no its not warapped in {}. just for understanding I used it. so i couldnt use strtok either.

example for the format of the data is:
5gterfs2rt5tyuri4oiuy10rtyuioiuyt

Probably easiest would be to read the length and then extract the characters after it one by one. The code is longer because Ed put in complete error checking:

#include <cctype>
#include <cstdlib>
#include <iostream>

int main()
{
  char *p = "6gterfs2rt5tyuri4oiuy10rtyuioiuyt";
  char *data = p;
  long length;

  while (*data != '\0') {
    // Save the current position for error checking
    char *temp = data;
    long length = std::strtol(data, &data, 0);

    // Make sure a leading length is present
    if (data == temp) {
      std::cerr << "Invalid length detected "
        "at [" << data << "]\n";
      break;
    }

    // Make sure trailing data is present
    if (*data == '\0') {
      std::cerr << "Length detected ["
        << length << "] without data\n";
      break;
    }

    for (long i = 0; *data != '\0' && i < length; ++i)
      std::cout << *data++;
    std::cout << '\n';

    // Make sure the data matches the length
    if (*data != '\0' && !std::isdigit(*data)) {
      std::cerr << "Bad length [" << length
        << "] before [" << data << "]\n";
      break;
    }
  }
}

Error checking is good. It's because of the error checking that Edward found an error in your sample string. The first data set doesn't match the length you gave "5gterfs". "gterfs" has 6 characters but the length only specifies 5. ;)

thanks edward.
The solution given by the Edward is woring fine. but it fails if data starts with a digit.
e.g. char *p = "006gterfs002rt005tyuri0028ab004oiuy010rtyuioiuyt";
here length of the data = 002 and data =8ab

and the other thing is here data will be display character by character.
I want to assigned the extracted data into a char pointer.

e.g. char * ptr;
and ptr = ab

> but it fails if data starts with a digit.
With the example you gave before: "5gterfs2rt5tyuri4oiuy10rtyuioiuyt", that's an impossible case because the length could be multiple digits. Your new example suggests that the length is a fixed width number of 3 digits with leading zeros. That's different, and it's a good way to see how minor differences in the format can change the suitability of a solution. ;) When you need help parsing a string, it's best to provide a formal specification and at least some of the actual data rather than a summary.

> here length of the data = 002 and data =8ab
That's still a logical error though. The length is 2, but the number of data characters is 3. Here's another example that extracts the data--without error checking!--using a 3 digit length:

#include <cstdio>
#include <cstdlib>
#include <iostream>

int main()
{
  char *p = "006gterfs002rt005tyuri0038ab004oiuy010rtyuioiuyt";
  char *data = p;

  while (*data != '\0') {
    char temp[4];
    int length;

    sscanf(data, "%3[0123456789]", temp);
    length = std::strtol(temp, 0, 10);
    data += 3;

    for (int i = 0; *data != '\0' && i < length; ++i)
      std::cout << *data++;
    std::cout << '\n';
  }
}

>and the other thing is here data will be display character by character.
How you process the characters after they're extracted is your business, not Ed's. The code was meant to be an example of how to extract the characters, not a complete solution to your problem.

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.