Solved

Searching a String

Posted on 2007-04-02
11
241 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
ID: 18840384
> 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
ID: 18840543
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
ID: 18840549
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
ID: 18840555
I'm sorry..you want:

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

Accepted Solution

by:
jkr earned 200 total points
ID: 18840777
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:directxBOB
ID: 18840861
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
ID: 18840872
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
ID: 18841605
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
ID: 18841874
>>>> 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
ID: 18841877
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
ID: 18845820
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

911 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

25 Experts available now in Live!

Get 1:1 Help Now