One-dimensional Japanese puzzle
Code with iterators and standard algorithms, and you'll see your coding style improve dramatically:


finding the beginning of a group of Bs can be done with std::find: auto b = std::find(first, last, 'B');
finding the end of a group of Bs can also be done with std::find: auto e = std::find(b, last, 'W');
computing the distance between the two is the job of std::distance: auto nb_bs = std::distance(b, e).


So combining all that we get:

#include &lt;vector&gt;
#include &lt;algorithm&gt;

template &lt;typename Iterator&gt;
auto groups_of_bs(Iterator first, Iterator last) {
    std::vector&lt;int&gt; result;
    while (true) {
        first = std::find(first, last, 'B');
        if (first == last) break;
        auto w = std::find(std::next(first), last, 'W');
        result.push_back(std::distance(first, w));
        if (w == last) break;
        first = std::next(w);
    }
    return result;
}


Now the only thing left is to display the size of the vector, and then its elements:

#include &lt;string&gt;
#include &lt;iostream&gt;

int main() {
    std::string test{"BBWBWB"};
    auto res = groups_of_bs(test.begin(), test.end());
    std::cout &lt;&lt; res.size() &lt;&lt; '\n';
    for (auto i : res) std::cout &lt;&lt; i &lt;&lt; ' ';
}