Searching a String

Just been playing around with a few string searches.

I have just done:

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

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

      return count;
}


The idea is that the user would pass in a string (*pString) and then an int (c) which is the location in that string of the first seperator. It will then search the string and count how many seperators I have, returning the count.

Does it look right? anything I could do to improve it?
directxBOBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nixfreakCommented:
> int (c) which is the location in that string of the first seperator.

c seems to contain the value of the seperator and not its location.
0
BrianGEFF719Commented:
nixfreak is exactly right, c is the value of the seperator...so technically instead of casting c to a char you should have the definition of the function be int (const char *, char). Where the return value is the number of occourences.
0
BrianGEFF719Commented:
And I think you want:

while(pString++); //I dont think you want to dereference then increment!
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

BrianGEFF719Commented:
I'm sorry..you want:

while ( *(++pString) );
0
jkrCommented:
>>anything I could do to improve it?

You could shorten it a bit, e.g.

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

  int count = 0;

  while(*pString++) if ((char) c == *pString) count++;

  return count;
}

The logic is correct. Apart from that, you might want to consider STL's 'count()':

#include <algorithm>
#include <functional>
#include <string>

using namespace std;

int String_CountSeperators(const char *pString, int c)
{
  string s = pString;

  return count(s.begin(),s.end(),(char)c);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
directxBOBAuthor Commented:
Sorry my mistake, yes I was referring to C as an Int being the location within the pString.

I'm still getting used to talking C and C++ (formerly a java programmer)
0
jkrCommented:
You're in good company, a lot of C runtime library functions (such as 'tolower()' etc.) take the char argument as an 'int', thus this did not seem important to me.
0
Infinity08Commented:
BrianGEFF719,

>> while(pString++); //I dont think you want to dereference then increment!

++ has higher precedence than * (dereference), so technically, *pString++ is correct since the increment refers to pString, and not to *pString.

But there's indeed something wrong : the increment is too late. Either use :

    while (*pString) {

        ++pString;
    }

or :

    do {

    while (*(++pString));   /* <----  prefix increment : first increment, then dereference */

The problem with the second solution is that the first character of the string is NOT checked. So, if an empty string is passed, you could have a problem (not in this simple code though).



jkr, your code should have been like this :

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

  int count = 0;

  while(*pString) if ((char) c == *pString++) count++;

  return count;
}

The increment was placed too early (it skipped the first character) ...
0
itsmeandnobodyelseCommented:
>>>> anything I could do to improve it?

Maybe that:
int String_CountSeperators(const char *pString, int c)
{
      int count = 0;
      while(pString = strchr(pString, c)) pString++, count++;
      return count;
}

or
 
int String_CountSeperators(const char *pString, int c)
{
     for (int count = 0; pString  != NULL; count += ((char)c == *pString++)? 1 : 0);  
     return count;
}
0
itsmeandnobodyelseCommented:
Some compilers need:    
     int count = 0;
     for (; pString  != NULL; count += ((char)c == *pString++)? 1 : 0);  
     return count;
0
BrianGEFF719Commented:
itsmeandnobodyelse:

I think you forgot to dereference pString:
>> for (; pString  != NULL; count += ((char)c == *pString++)? 1 : 0);  

for (; *pString  != NULL; count += ((char)c == *pString++)? 1 : 0);  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.