473,769 Members | 7,315 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6493


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.c om, 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
2461
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
1238
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
1645
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 matching LHS related to that non_terminal; i want to allow the user to select a rule (from # key)they want to use. When the user selects a specified rule; i need to replace the rule in that present address by expanding the non_terminal. I was...
3
4551
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 = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
2
3102
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 parameter has the 'int' type. From my knowledge it is not even portable. On a machine where 'char' has the same size as an 'int' and where 'char' has the same
4
1727
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 muliple occurances of the record and leave it so that there is only one record of each occurance remaining. I am aware that there is a feature in the query wizard to delete multiple occurances but I dont want to delete all occurances - just
10
1898
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, 'b' to ter... ... 2nd string to to ter , 'b' to ter... ... here are the codes: ... int i, k; char str={"adevf", "dcdhn", "oledc", "brdca"}; char chr={'a', 'b', 'c'};
25
4556
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 this. So of course it does not matter how many bits a single int has, but I do need a reliable way to find it out. I think of something like
5
1561
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 ( italicized) alignment restrictions" I have looked at the threads about alignment in this group, and most **assume** that the writer/replies understand fully what alignment entails. There are some quite good explanations on the web, but none really...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.