Hello everyone. I have a question and I do not know how to solve.
Think a string like {11111144444445555555556666666} and I want output as
{61749576} (6 times 1, 7 times 4....)
If you are not concerned about the location of the repeated consecutive digits, or even if repeated digits are next to each other, then simply counting the number of each individual digit is easy:
1. create an array/vector to hold the number of each discrete digit, sizing it to be 10 elements. Digits 0 through 9, array/vector index 0-9.
2. loop through the individual elements of the string. Note what digit the element is. Increment the appropriate array/vector element.
3. loop through the elements of your array/vector and output the index and the contained number.
How you loop through the string depends on whether it is a C-style char array or C++ string. Minor differences only, the indexing logic is much the same.
I got a error like to_string was not declared in scope
Set your compiler to use (at least) c++11.
(In their current positions, and relying on counter always being less than or equal to 9) you could replace both occurrences of to_string() with (char)( '0' + counter )
or you could write your own version of to_string() using, e.g., stringstream. However, both these approaches are suboptimal. You would be much better in the long run either upgrading your compiler or setting one of its options so that you can write (at least) c++11 code.
In the meantime you can try the code in cpp.sh using the little gear-wheel icon to top-right of the code sample.