Solved

Searching a String

Posted on 2007-04-02
11
227 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:directxBOB
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 7

Expert Comment

by:nixfreak
Comment Utility
> 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
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
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
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
And I think you want:

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

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 200 total points
Comment Utility
I'm sorry..you want:

while ( *(++pString) );
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
>>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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:directxBOB
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
Comment Utility
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Some compilers need:    
     int count = 0;
     for (; pString  != NULL; count += ((char)c == *pString++)? 1 : 0);  
     return count;
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 200 total points
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now