473,386 Members | 1,720 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,386 software developers and data experts.

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

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
7 10017
"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
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

"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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
10
by: Jon | last post by:
I want to count the number of instances of a certain string(delimiter) in another string. I didn't see a function to do this in the framework (if there is, please point me to it). If not, could...
3
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
3
by: chrisperkins99 | last post by:
It seems to me that str.count is awfully slow. Is there some reason for this? Evidence: ######## str.count time test ######## import string import time import array s = string.printable *...
3
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the...
2
by: Maxington | last post by:
The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n" string. I have a string that has "\n" within it and I...
3
by: magix | last post by:
How can I search for occurance of a character in certain position of a string I checked function strchr, but doesn't option to specify position. Thanks. Regards, Magix
2
by: jamesnkk | last post by:
Hi, I have a ms-access table and i want to count the failure occurance. BOARD-ID....FAILURE A-123.......... FAIL-A A-123.......... FAIL-A A-125..........FAIL-A A-126.......... FAIL-A ...
9
by: john20 | last post by:
Hi All, How can i find the total 'Count' of a particular character's occurance in a string. means i want to find the count of '<' in a string. can you please help to find that. Thanks
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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.