How can I count the number of "_" in a string like "bla_bla_blabla_bla"?
13 Answers
#include <algorithm> std::string s = "a_b_c"; size_t n = std::count(s.begin(), s.end(), '_'); 6Pseudocode:
count = 0 For each character c in string s Check if c equals '_' If yes, increase count EDIT: C++ example code:
int count_underscores(string s) { int count = 0; for (int i = 0; i < s.size(); i++) if (s[i] == '_') count++; return count; } Note that this is code to use together with std::string, if you're using char*, replace s.size() with strlen(s).
Also note: I can understand you want something "as small as possible", but I'd suggest you to use this solution instead. As you see you can use a function to encapsulate the code for you so you won't have to write out the for loop everytime, but can just use count_underscores("my_string_") in the rest of your code. Using advanced C++ algorithms is certainly possible here, but I think it's overkill.
Old-fashioned solution with appropriately named variables. This gives the code some spirit.
#include <cstdio> int _(char*__){int ___=0;while(*__)___='_'==*__++?___+1:___;return ___;}int main(){char*__="_la_blba_bla__bla___";printf("The string \"%s\" contains %d _ characters\n",__,_(__));} Edit: about 8 years later, looking at this answer I'm ashamed I did this (even though I justified it to myself as a snarky poke at a low-effort question). This is toxic and not OK. I'm not removing the post; I'm adding this apology to help shifting the atmosphere on StackOverflow. So OP: I apologize and I hope you got your homework right despite my trolling and that answers like mine did not discourage you from participating on the site.
15#include <boost/range/algorithm/count.hpp> std::string str = "a_b_c"; int cnt = boost::count(str, '_'); Using the lambda function to check the character is "_" then the only count will be incremented else not a valid character
std::string s = "a_b_c"; size_t count = std::count_if( s.begin(), s.end(), []( char c ){return c =='_';}); std::cout << "The count of numbers: " << count << std::endl; 7You name it... Lambda version... :)
using namespace boost::lambda; std::string s = "a_b_c"; std::cout << std::count_if (s.begin(), s.end(), _1 == '_') << std::endl; You need several includes... I leave you that as an exercise...
9Count character occurrences in a string is easy:
#include <bits/stdc++.h> using namespace std; int main() { string s="Sakib Hossain"; int cou=count(s.begin(),s.end(),'a'); cout<<cou; } 2There are several methods of std::string for searching, but find is probably what you're looking for. If you mean a C-style string, then the equivalent is strchr. However, in either case, you can also use a for loop and check each character—the loop is essentially what these two wrap up.
Once you know how to find the next character given a starting position, you continually advance your search (i.e. use a loop), counting as you go.
I would have done this way :
#include <iostream> #include <string> using namespace std; int main() { int count = 0; string s("Hello_world"); for (int i = 0; i < s.size(); i++) { if (s.at(i) == '_') count++; } cout << endl << count; cin.ignore(); return 0; } 0You can find out occurrence of '_' in source string by using string functions. find() function takes 2 arguments , first - string whose occurrences we want to find out and second argument takes starting position.While loop is use to find out occurrence till the end of source string.
example:
string str2 = "_"; string strData = "bla_bla_blabla_bla_"; size_t pos = 0,pos2; while ((pos = strData.find(str2, pos)) < strData.length()) { printf("\n%d", pos); pos += str2.length(); } I would have done something like that :)
const char* str = "bla_bla_blabla_bla"; char* p = str; unsigned int count = 0; while (*p != '\0') if (*p++ == '_') count++; Try
#include <iostream> #include <string> using namespace std; int WordOccurrenceCount( std::string const & str, std::string const & word ) { int count(0); std::string::size_type word_pos( 0 ); while ( word_pos!=std::string::npos ) { word_pos = str.find(word, word_pos ); if ( word_pos != std::string::npos ) { ++count; // start next search after this word word_pos += word.length(); } } return count; } int main() { string sting1="theeee peeeearl is in theeee riveeeer"; string word1="e"; cout<<word1<<" occurs "<<WordOccurrenceCount(sting1,word1)<<" times in ["<<sting1 <<"] \n\n"; return 0; } public static void main(String[] args) { char[] array = "aabsbdcbdgratsbdbcfdgs".toCharArray(); char[][] countArr = new char[array.length][2]; int lastIndex = 0; for (char c : array) { int foundIndex = -1; for (int i = 0; i < lastIndex; i++) { if (countArr[i][0] == c) { foundIndex = i; break; } } if (foundIndex >= 0) { int a = countArr[foundIndex][1]; countArr[foundIndex][1] = (char) ++a; } else { countArr[lastIndex][0] = c; countArr[lastIndex][1] = '1'; lastIndex++; } } for (int i = 0; i < lastIndex; i++) { System.out.println(countArr[i][0] + " " + countArr[i][1]); } } 2