strchr & finding multiple occurances of a char

D

David Warner

Greetings!

In looking into some C coding, I am looking for the C function that will
search for multiple occurances of a same character in a string.

For Instance:

char str[80] = "We the people of the United States";

strchr will be successful if i am looking for the "U". It returns a
successful condition.

However, if I want to parse down the string and look for each occurance
of "o", I should either get a count of 2 or the pointer moves to the
first occurance.

Is there a standard C function that returns the value(s) i am looking
for or is this something I need to write myself?

Thanks in advance for your assistance!
 
E

Eric Sosman

David said:
Greetings!

In looking into some C coding, I am looking for the C function that will
search for multiple of a same character in a string.

For Instance:

char str[80] = "We the people of the United States";

strchr will be successful if i am looking for the "U". It returns a
successful condition.

However, if I want to parse down the string and look for each occurance
of "o", I should either get a count of 2 or the pointer moves to the
first occurance.

I understand the "count of 2" piece, but I'm not at all
sure what you mean by "or the pointer moves to the first
occurance [sic]."
Is there a standard C function that returns the value(s) i am looking
for or is this something I need to write myself?

You could use strchr() in a loop, finding one target
character each time (except the last):

int count = 0;
char *where = str;
while ((where = strchr(where, 'o')) != NULL) {
++count;
++where;
}

Personally, I'd probably opt for the simpler

for (where = str; *where != '\0'; ++where) {
if (*where == 'o')
++count;
}

There's a chance the former version might be faster, especially
if `str' is very long and contains few occurrences of the target
character. However, the difference is unlikely to be significant
in the face of whatever else the program is doing (I/O, for
example), while the second is easier to read and perhaps harder
to botch.
 
A

Arthur J. O'Dwyer

In looking into some C coding, I am looking for the C function that will
search for multiple occurances of a same character in a string.

For Instance:

char str[80] = "We the people of the United States";

strchr will be successful if i am looking for the "U". It returns a
successful condition.

However, if I want to parse down the string and look for each occurance
of "o", I should either get a count of 2 or the pointer moves to the
first occurance.

That's right. Where's the problem?

char *p;
p = strchr(str, 'e');
while (p != NULL) {
/* do something */
something(p);
/* get the next 'e' */
p = strchr(p, 'e');
}
/* now all the 'e's have been processed */

-Arthur
 
P

Peter Nilsson

Arthur said:
... Where's the problem?

char *p;
p = strchr(str, 'e');
while (p != NULL) {
/* do something */
something(p);
/* get the next 'e' */
p = strchr(p, 'e');

ITYM: p = strchr(p + 1, 'e');
 
C

CBFalconer

Arthur J. O'Dwyer said:
That's right. Where's the problem?

char *p;
p = strchr(str, 'e');
while (p != NULL) {
/* do something */
something(p);
/* get the next 'e' */
p = strchr(p, 'e');
p = strchr(p+1, 'e');
}
/* now all the 'e's have been processed */

The above change might reduce the running time when 'e' is present
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top