C++ program for run Length Encoding and Decoding

Run-length encoding (RLE) is a straightforward method of data compression that substitutes a single element followed by a count of how many times it repeats for a series of identical elements (such as letters or numbers). There are the following steps:

1. Encoding

  • The input data is scanned from left to right while it is being encoded.
  • A single element that is followed by a count is created by condensing several identical elements into one.
  • A number is frequently used to signify the count.

2. Decoding

  • In the decoding procedure, the encoded data is scanned from left to right.
  • The following character is replicated whenever a numerical count is found.
  • It keeps doing this until all of the encoded data has been unlocked.

3. Programming Language in C++ for Run-Length Encoding and Decoding

  • The C++ program that is provided demonstrates the Run-Length Encoding and Decoding. There are two primary purposes for it:
    1. encodingRLE(const string, input)
      • Returns the RLE-encoded equivalent of an input string.
      • Counts a sequence of identical characters by iterating through the supplied string.
      • Adds every character along with its count to the encoded string.
    2. decodeRLE(const string; encoded)
      • Produces a decoded version of an RLE-encoded string when given one.
      • Repeats characters from the encoded string based on their counts as it iterates over it.
      • Append the duplicated characters to the decoded string.

4. Usage

  • An input string is provided by the user.
  • EncodeRLE is used by the program to encrypt the input.
  • After that, the program decodes the encoded string through the use of decodeRLE.
  • Printing is done with both the unencrypted and encoded versions.

5. Applications

  • RLE is a quick and effective compression method that is used for both image and video compression, particularly for lossless compression of binary pictures.
  • Additionally, it is employed in situations when there are several instances of consecutively repeated elements, such as when compressing text files because it is an effective way to encrypt repeated characters or spaces.

Code:

Output:

Enter a string: Encourage
Encoded: E1n1c1o1u1r1a1g1e1
Decoded: Encourage

Explanation:

  1. In this example, we use encodeRLE for encoding and decodeRLE for decoding are the two primary components of the program.
  2. Within the encodeRLE function
    • The result of the encoding is stored in an initialized empty string that has been encoded.
    • It initializes a count variable to track a string of identical characters.
    • Each character is compared to the one before it as it loops over the input string.
    • It increases the count if a character is the same as the one before it.
    • If the character is different, it adds the prior character and its count to the encoded string, resets the count to 1, and adds the new character.
    • It adds the final character and its count to the encoded string after the loop.
  3. Within the decodeRLE function
    • It initializes an empty decoded string to store the decoded outcome.
    • It cycles through the encoded string.
    • It reads the character and any associated count (if any) for each character before appending the character to the decoded count.
  4. The main function
    • It accepts user input in the form of a string.
    • It uses the encodeRLE function to encode the input string.
    • It invokes the decodeRLE function to decode the encoded string.
    • It prints both the encoded and the decoded strings.