Link to home
Start Free TrialLog in
Avatar of directxBOB
directxBOBFlag for Ireland

asked on

Counting the number of tokens in a String.

I've been messing around trying to get the most efficient possible method and here is what I have:

const char *String_Contains(const char *s1, const char *s2)
{
  int s1Len = String_Length(s1);
  int s2Len = String_Length(s2);
  for(; s1Len >= s2Len; ++s1, --s1Len)
  {
    if(String_BeginsWith(s1, s2))
      return s1;
  }
  return NULL;
}

What I do is pass in 2 strings, 1 is a full string, while the other is a string containing seperators. Then I loop through and count the number of times I find a seperator and return them as a char.

Any improvements or errors that I may have
Avatar of ozo
ozo
Flag of United States of America image

I don't see where you count the number of times you find a seperator
Avatar of directxBOB

ASKER

Sorry the count is another method, I've been trying to change the code so it's far more re-usable.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok well I can use:

int String_FindDelimiter(const char *pString, const char *pDelimiters)
{
  int i,j;
  for (i=0;pString[i];++i)
  {
    for (j=0;pDelimiters[j];++j)
      if (pString[i] == pDelimiters[j])
        return i;
  }
  return i; // No delimiter found, return remaining string length
}

This will return the position of the delimiter in the character list, thus I guess I could pass in that string into:

int countseperators (const char *s, const char *charset){
  int count=0;
  while( s=strpbrk(s, charset) ){ s++; count++; }
  return count;
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How would I fit the above calls into the following:

    pCE->pEventCallback(pEditorScript, pAIEv->pParameter ? 1 : 0, pAIEv->pParameter ? &pAIEv->pParameter : NULL, pCE->pUserData);

so basically I want to find what the seperator is, then use it and count the number of seperators, and use that in:

    pCE->pEventCallback(pEditorScript, pAIEv->pParameter ? 1 : 0, pAIEv->pParameter ? &pAIEv->pParameter : NULL, pCE->pUserData);

where in pCE->pEventCallback(pEditorScript, pAIEv->pParameter ? 1 : 0, pAIEv->pParameter ? &pAIEv->pParameter : NULL, pCE->pUserData); do you want to use the number of separators?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where in pCE->pEventCallback(pEditorScript, pAIEv->pParameter ? 1 : 0, pAIEv->pParameter ? &pAIEv->pParameter : NULL, pCE->pUserData); do you want to use the number of separators?



Well I was of the assumption that the best place to use it would be where 1 is, so that there can be more than 1 Parameter. I really hate how the above is written but have to make do.