Is there better way to increment the count every 10, 100, 1000, ...?

The problem I faced with this idea was when I was supposed to count the length of some string.

If I get aaa...a (a times 10), this string compressor makes it into "10a".

If I get 'a' * 100, I would get "100a" and so on.

Instead of creating string "100a", I counted the length 4 by using the code under, but I wonder if there's better way to implement this because running switch for every time I increment the count seems redundant and ineffective.

1
2
3
4
5
6
7
            count += 1;
            switch (count)
            {
                case 2: cur_length++; break;
                case 10: cur_length++; break;
                case 100: cur_length++; break;
            }


Can anyone give me any idea about this?

Thank you.
Last edited on
Presumably, your compressor can output "7a" "42a" "451a" or indeed any other integer number it likes.

What you need to do is separate the value of n from an "na" token, then it's just
for ( i = 0 ; i < n ; i++ )
std::string type already has the length of string.
What is your problem?


1
2
3
4
5
6
7
8
9

std::string str; 
/* 
   initialize string to be your target value
*/

auto output = std::to_string(str.size()) + str;

Last edited on
if you actually had to have this logic (see above comments on that)
remove the breaks and add a default and it would be very efficient.

1
2
3
4
5
6
7
 switch (count)
            {
                case 2: 
                case 10: 
                case 100: cur_length++; break;
                default:  ;
            }
Last edited on
Use std::to_string to convert a number to a string. https://en.cppreference.com/w/cpp/string/basic_string/to_string

Use a string stream https://en.cppreference.com/w/cpp/io/basic_istringstream
or std::stoull https://en.cppreference.com/w/cpp/string/basic_string/stoul
to convert the characters in the string to an unsigned integer value

For example:

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
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <sstream>

// we assume that str does not contain decimal digits
std::string encode( const std::string& str )
{
    if( str.size() < 2 ) return str ;

    std::string result ;

    char curr_char = str.front() ;
    std::size_t curr_char_cnt = 1 ;

    for( std::size_t i = 1 ; i < str.size() ; ++i )
    {
        if( str[i] == curr_char ) ++curr_char_cnt ;

        else
        {
            result += std::to_string(curr_char_cnt) + curr_char ;
            curr_char = str[i] ;
            curr_char_cnt = 1 ;
        }
    }

    return result + std::to_string(curr_char_cnt) + curr_char ;
}

std::string decode( const std::string& str )
{
    std::string result ;

    std::istringstream sstm(str) ;

    std::size_t n ;
    char c ;
    while( sstm >> n && sstm.get(c) )
        result += std::string( n, c ) ; // append a string containing n copies of c

    return result ;
}

int main()
{
    const std::string str = "abbbccccccccccccdeeeeeeeeeeefgggggggg" ;

    std::cout << str << '\n'                      // abbbccccccccccccdeeeeeeeeeeefgggggggg
              << encode(str) << '\n'              // 1a3b12c1d11e1f8g 
              << decode( encode(str) ) << '\n' ;  // abbbccccccccccccdeeeeeeeeeeefgggggggg
}

http://coliru.stacked-crooked.com/a/adb76e1ea30c5b56
@jonnin
Wow, this was something I've couldn't thought of before. Thank you.

@salem c
@Mitsuru
My work was supposed to get the string and get the length of the compressed string.
I thought not actually having the compressed string it self might have better performance for I won't be needing the memory allocation.
Haven't thought of using length function of string. Thank you

@JLBorges
My problem was that there character which doesn't get repeated should be in form of itself.
i.e.) 'abcdee' => 'abcd2e'
But, using stringstream was something I haven't thought of because I wasn't even thinking about decoding it!
your idea looks great and I think I got much out from it. Thank you.
how do you compress 11111111111 (11 1s) :)
Use the standard solution: escape sequences.

For instance, with the customary \ as the escape character, 11111111111\333 is compressed to 11\1\\3\3
Topic archived. No new replies allowed.