Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Searching a String

Posted on 2007-04-02
11
Medium Priority
?
268 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
[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
  • 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
Industry Leaders: 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!

 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 800 total points
ID: 18840555
I'm sorry..you want:

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

Accepted Solution

by:
jkr earned 800 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 400 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 800 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

715 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