By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,704 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,704 IT Pros & Developers. It's quick & easy.

how to count the occurance of a character in a string ?

P: n/a
how to count the occurance of a character in a string ?

e.g.
#include <iostream>
#include <string>
using namespace std;
int main (){
string text = "abc, def, ghi, jkl, mno";
// how to count the number of occurance of "," in text ????
return 0;
}

thanks
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:408a92c6
how to count the occurance of a character in a string ?


std::count
Jul 22 '05 #2

P: n/a
but it said "count" is undeclared identifier......
shall i include any library or do anything?
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:sU********************@bgtnsc04-news.ops.worldnet.att.net...
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:408a92c6
how to count the occurance of a character in a string ?


std::count

Jul 22 '05 #3

P: n/a

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.hku.hk...
how to count the occurance of a character in a string ?

e.g.
#include <iostream>
#include <string>
using namespace std;
int main (){
string text = "abc, def, ghi, jkl, mno";
// how to count the number of occurance of "," in text ????
return 0;
}

thanks


It's very simple, in fact it couldn't be easier. Loop though the string
looking for commas.

int count = 0;
for (int i = 0; i < text.size(); ++i)
if (text[i] == ',')
++count;

john
Jul 22 '05 #4

P: n/a
news.hku.hk wrote:
how to count the occurance of a character in a string ?

e.g.
#include <iostream>
#include <string>
using namespace std;

Think twice before u 'use' the entire namespace. It would cause name
pollution which a probable user of the API won't even notice.

Aliter:

#include <string>
#include <iostream>

typedef unsigned int UINT;

UINT ncount(std::string text, char ch) {
UINT count = 0;

int index = -1;
while (1) {
index = text.find_first_of(ch, index + 1);
if (index == text.npos) break;
else count++;
}
return count;

}

Karthik
------
Human Beings please 'removeme' for my email.
Jul 22 '05 #5

P: n/a
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:<40********@newsgate.hku.hk>...
how to count the occurance of a character in a string ?

e.g.
#include <iostream>
#include <string>
using namespace std;
int main (){
string text = "abc, def, ghi, jkl, mno";
// how to count the number of occurance of "," in text ????
return 0;
}

thanks


Learn to use the elements of the algorithms package for stuff like
this; it gives you some great high-level methods.
http://www.josuttis.com/libbook/toc.html is the best site I've found
covering these things.

For this problem use count;

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main (){
string text = "abc, def, ghi, jkl, mno";

//num_commas holds the number of elements in text == ','
int num_commas = count(text.begin(), text.end(), ',');

return 0;
}
Jul 22 '05 #6

P: n/a
On Sun, 25 Apr 2004 13:00:23 +0800, "news.hku.hk"
<bi******@hkusua.hku.hk> wrote:
but it said "count" is undeclared identifier......
shall i include any library or do anything?
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:sU********************@bgtnsc04-news.ops.worldnet.att.net...
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:408a92c6
> how to count the occurance of a character in a string ?


std::count


You need to include <algorithm> for std::count()
The STL is described at http://www.sgi.com/tech/stl/

#include <iostream>
#include <string>
#include <algorithm>

int main ()
{
std::string text = "abc, def, ghi, jkl, mno";
std::cout << "Number of commas = "
<< std::count(text.begin(), text.end(), ',')
<< '\n';
return EXIT_SUCCESS;
} // end main()

### Warning - not compiled so may have small errors. ###

rossum
--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #7

P: n/a
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message news:<sU********************@bgtnsc04-news.ops.worldnet.att.net>...
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:408a92c6
how to count the occurance of a character in a string ?


std::count


I've noticed a number of people posting alternatives. std::count
is superior, for a number of reasons:

* It's tested
* It's documented
* Someone reading the code later immediately sees the intent
* It may be optimized for your particular implementation
using non-portable tricks (e.g. written in assembly).

The last argument is slightly tricky. You could write similar
optimized code, but then the first three points apply even more.
Worse, your code would be non-portable. Had you used std::count,
you would get a different std::count when you actually port.

Of course, std::count is O(n) so performance is good enough unless
profiling proves otherwise.

Regards,
Michiel Salters
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.