473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strchr & finding multiple occurances of a char

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!
Nov 14 '05 #1
4 6388


David Warner wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #2

On Mon, 25 Apr 2005, David Warner wrote:

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
Nov 14 '05 #3
Arthur J. O'Dwyer wrote:
On Mon, 25 Apr 2005, David Warner wrote:

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.
... 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');
}
/* now all the 'e's have been processed */


--
Peter

Nov 14 '05 #4
"Arthur J. O'Dwyer" wrote:
On Mon, 25 Apr 2005, David Warner wrote:
.... snip ...
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');

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
:-)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Servé Lau | last post by:
Sometimes I code like this: char *p = strchr(buf,'\n'); if (p) *p = 0; But I've also seen the shorter expression: buf = 0;
2
by: Peter | last post by:
What is the most efficient way to count occurances of charaters in a string? IE. Searching for ',' in a comma del file.
0
by: Willing 2 Learn | last post by:
I'm working on this program below but im stuck. after it finds a match b/ween sentence & non_terminal it will output where these are found in both strings. I need it to spit out the rules with a...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
2
by: ts | last post by:
Hello all, Can somebody explain why strchr is declared the way it is? Here is the declaration: char *strchr(const char *s, int c); Mainly I do not understand why the second ...
4
by: Chris | last post by:
Hi, I have imported a spreadsheet into an access databsae and have noticed that there are multiple occurances of exactly the same record. I would like to know if there is a way of deleting the...
10
by: runsun | last post by:
four strings are in a 2-D array; use strchr to find characters a, b, c, respectively; search results are to be put in another 2-D arrray ter: results of searching 'a' in the 1st string go to ter,...
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
5
by: mdh | last post by:
The 3rd paragraph says: "Alignment requirements can generally be satisfied easily, at the cost of some wasted space, by ensuring that the allocator always return a poiner that meets all (...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.