Search code examples
c++compressionrun-length-encoding

Run-length decompression using C++


I have a text file with a string which I encoded.

Let's say it is: aaahhhhiii kkkjjhh ikl wwwwwweeeett

Here the code for encoding, which works perfectly fine:

void Encode(std::string &inputstring, std::string &outputstring)
{
    for (int i = 0; i < inputstring.length(); i++) {
        int count = 1;
        while (inputstring[i] == inputstring[i+1]) {
            count++;
            i++;
        }
        if(count <= 1) {
            outputstring += inputstring[i];
        } else {
            outputstring += std::to_string(count);
            outputstring += inputstring[i];
        }
    }
}

Output is as expected: 3a4h3i 3k2j2h ikl 6w4e2t

Now, I'd like to decompress the output - back to original.

And I am struggling with this since a couple days now.

My idea so far:

void Decompress(std::string &compressed, std::string &original)
{
    char currentChar = 0;
    auto n = compressed.length();
    for(int i = 0; i < n; i++) {

        currentChar = compressed[i++];

        if(compressed[i] <= 1) {
            original += compressed[i];
        } else if (isalpha(currentChar)) {
            //
        } else {
            //
            int number = isnumber(currentChar).....
            original += number;
        }
    }
}

I know my Decompress function seems a bit messy, but I am pretty lost with this one. Sorry for that.

Maybe there is someone out there at stackoverflow who would like to help a lost and beginner soul.

Thanks for any help, I appreciate it.


Solution

  • #include "string"
    #include "iostream"
    
    
    void Encode(std::string& inputstring, std::string& outputstring)
    {
        for (unsigned int i = 0; i < inputstring.length(); i++) {
            int count = 1;
            while (inputstring[i] == inputstring[i + 1]) {
                count++;
                i++;
            }
            if (count <= 1) {
                outputstring += inputstring[i];
            }
            else {
                outputstring += std::to_string(count);
                outputstring += inputstring[i];
            }
        }
    }
    
    bool alpha_or_space(const char c)
    {
        return isalpha(c) || c == ' ';
    }
    
    void Decompress(std::string& compressed, std::string& original)
    {
        size_t i = 0;
        size_t repeat;
        while (i < compressed.length())
        {
            // normal alpha charachers
            while (alpha_or_space(compressed[i]))
                original.push_back(compressed[i++]);
    
            // repeat number
            repeat = 0;
            while (isdigit(compressed[i]))
                repeat = 10 * repeat + (compressed[i++] - '0');
    
            // unroll releat charachters
            auto char_to_unroll = compressed[i++];
            while (repeat--)
                original.push_back(char_to_unroll);
        }
    }
    
    int main()
    {
        std::string deco, outp, inp = "aaahhhhiii kkkjjhh ikl wwwwwweeeett";
    
        Encode(inp, outp);
        Decompress(outp, deco);
    
        std::cout << inp << std::endl << outp << std::endl<< deco;
    
        return 0;
    }