Solved

Creating a Character Pointer Array?

Posted on 2007-04-03
7
1,128 Views
Last Modified: 2012-06-27
I've been working on messing with some strings, what I have at the momment is:

int String_CountSeperators(const char *pString, int c)
{

  int count = 0;
      do
      {
                if(*pString == (char)c)
                    {
                       count++;
                     }
      }
      while(*pString++);

      return count;
}



How would I convert this so that I would pass in a seperator (C is the location of the first seperator) then store a pointer to the first position of each new param (basically the position just after each seperator).

I would assume I would need an array, and as I iterate through this, I would have to place the pointer to each param into the array.

(Again I am probably talking C# or Java terms, so if I am not making sense please just ask me to re-explain)
0
Comment
Question by:directxBOB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18848040
The easiest idea would be to not reinvent the wheel and use 'strtok()', which does exactly that, i.e.

#include <string.h>

char** String_GetSubstringFromSeperators(const char *pString, int c)
{

  int count = String_CountSeperators(pString,c);
  char** pSubs = new char*[count];

  char seps[2];
  sprintf(seps,"%c",(char)c);

  char* token = strtok(pString,seps);
  int n = 0;

  while(token != NULL) {

    pSubs[i] = token;

    token = strtok(NULL,seps);

    ++i;

  }

  return pSeps;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 18848052
Oh, and also a pure C++ solution:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int split_text(string strIn, const char cDelim, vector<string>& vResult) {

   int nPos = 0;
   int nCount = 0;
   int nFound;
   string strToken;

   while(1) {

      nFound = strIn.find(cDelim,nPos);

      if (-1 == nFound)  {

        strToken = strIn.substr(nPos,strIn.length() - nPos);
        vResult.push_back(strToken);
        break;
      }

      strToken = strIn.substr(nPos,nFound - nPos);

      nPos = nFound + 1;

      ++nCount;

      vResult.push_back(strToken);

   }

   return nCount;
}

int main () {

  vector<string> vResult;

   int n = split_text("12456256|name|354||m|MN",'|',vResult);

   for ( int i = 0; i < n; ++i)
      cout << vResult[i] << endl;
}
0
 

Author Comment

by:directxBOB
ID: 18848247
trying to do this all without using things like strtok, as I want to really try and get to grips with working faster and more efficiently with C / C++
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 18848265
Not using 'strtok()' is not a big deal:

char** String_GetSubstringFromSeperators(const char *pString, int c)
{

  int count = String_CountSeperators(pString,c);
  char** pSubs = new char*[count];
  int n = 0;

  char* pLast = pString;

  while(*pString++) {

    if ( *pString = (char)c) {

        *pString = '\0'; // null-terminate substring
        pSubs[i] = pLast;
        ++i;
        pLast = pString;
    }
  }

  return pSeps;
}
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 250 total points
ID: 18856367
The answer is to pass an array of char * to hold the list. Something like:

int String_CountSeperators(const char *pString, int c, char * params[], int max)
{

  int count = 0;
      do
      {
                if(*pString == (char)c)
                    {
                       params[count++] = pString+1;
                     }
      }
      while(*pString++ && count < max);

      return count;
}

But remember that each parameter will not be null-terminated.

Paul
0
 

Author Comment

by:directxBOB
ID: 19205384
Sorry for the delay in this lads, I wasn't able to get it working and got sidetracked with build machines. Anyway I split the points and again my apologies for the delay.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question