Solved

Searching a String

Posted on 2007-04-02
11
249 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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