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

N

news.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
 
N

news.hku.hk

but it said "count" is undeclared identifier......
shall i include any library or do anything?
 
J

John Harrison

news.hku.hk said:
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 == ',')
++count;

john
 
K

Karthik

news.hku.hk said:
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
 
J

James Moughan

news.hku.hk said:
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;
}
 
R

rossum

but it said "count" is undeclared identifier......
shall i include any library or do anything?

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
 
M

Michiel Salters

Siemel Naran said:
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
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top