Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Counting the number of tokens in a String.

Posted on 2007-03-29
9
Medium Priority
?
357 Views
Last Modified: 2010-04-15
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
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
  • 4
  • 4
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18820806
I don't see where you count the number of times you find a seperator
0
 

Author Comment

by:directxBOB
ID: 18820853
Sorry the count is another method, I've been trying to change the code so it's far more re-usable.
0
 
LVL 84

Accepted Solution

by:
ozo earned 1800 total points
ID: 18820869
You can count separators with
int countseperators (const char *s, const char *charset){
  int count=0;
  while( s=strpbrk(s, charset) ){ s++; count++; }
  return count;
}
but that may not be the same as the number of tokens if there can be multiple separators between tokens or leading or trailing tokens
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:directxBOB
ID: 18820908
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;
}
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 1800 total points
ID: 18820925
If you are going to do this many times with the same set of delimiters you might set up an array do you can do
if( isdelimiter[pString[i]] )
0
 

Author Comment

by:directxBOB
ID: 18821049
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);

0
 
LVL 84

Expert Comment

by:ozo
ID: 18821752
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?
0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 200 total points
ID: 18825117
Hello directxBOB,

If you really want to most efficient way, you should check out other algorithms.  Some of them are quite a drastic change from what we're taught and what you're probably used to.

The Boyer-Moore algorithm is exceptionally good on very long text.  Because of the start-up overhead, it's not particularly effective on short buffers.  Then again, for short buffers, the time required even for brute force is imperceptible to the human eye.

Here's a link to a pretty good description.  You can google "Boyer Moore" for lots more.

  http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm

Good Luck,
Kent
0
 

Author Comment

by:directxBOB
ID: 18833565
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.
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

609 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