Question

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....)

Last edited on
It won't work.

What happens when a number repeats more than 10 times and you start writing things like
1112
?

Is that
111 times 2
or
1 times 1, 1 times 2
etc.?

You either need some restriction on unit counts (e.g. less than or equal to 9) or some character to divide collections.

If you don't have a lot of repeats then your string may actually end up longer.
Last edited on
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.
Thank you for your reply. The maximum repeat value is 9.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
using namespace std;

string encode( const string &str )
{
   if ( str.empty() ) return "";

   string result;
   char c = str[0];
   int counter = 1;
   for ( int pos = 1; pos < str.size(); pos++ )
   {
      if ( str[pos] == c )
      {
         counter++;
         if ( counter == 10 )
         {
            result = result + '9' + c;
            counter = 1;
         }
      }
      else
      {
         result = result + to_string(counter) + c;
         c = str[pos];
         counter = 1;
      }
   }
   return result + to_string(counter) + c;
}


int main()
{
   string test = "11111144444445555555556666666";
// string test = "1111114444444555555555556666666";  // to test more than 10

   cout << "Original: " << test << '\n';
   cout << "Encoded:  " << encode( test ) << '\n';
}


Original: 11111144444445555555556666666
Encoded:  61749576
I got a error like to_string was not declared in scope
bhaci wrote:
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.
Last edited on
Topic archived. No new replies allowed.