Write a program that converts a sequence of bits into messages (this process is known as decoding, and the reverse process is called encoding). There are five types of bit encoding: 00, 01, 100, 1010, 1011. The corresponding message for each bit encoding is shown below.
00 -> Red
01 -> Green
100 -> Blue
1010 -> Pink
1011 -> Orange
Example 1 (click to see the figure):
Enter a sequence of bits: 10000101001001011
Blue Red Pink Green Red Orange
In Example 1,
program input: 10000101001001011
program output: Blue Red Pink Green Red Orange
More input-output samples.
The input sequence of bits is decoded from left to right.
You can assume that all the input sequences are leagal.
The last output character can be a space. That is, the following program output for Example 1 is also legal:
Blue Red Pink Green Red Orange
It is recommended that your solution is written based on the code template.
Do not output any line breaks except the one in the code template.
Do not modify the cout statements in the code template.
Do not use getline(cin, code); otherwise your assignment will not be processed.
To check whether an input bit is 0, use code[i] == '0' or code.at(i) == '0'
Treat code as a character array, where each element represetns a single character.
Remember that the character '0' is distinct from the integer 0.
Using code.at(i) is preferable to code[i], because if index i is out of range, code.at(i) will throw an error, while code[i] will not detect this issue.
Make sure that the array index is within its valid range.
When using code[i], ensure that i >= 0 and i <= code.length()-1
When using code[i+1], ensure that i+1 >= 0 and i+1 <= code.length()-1
Accessing an index outside of this range can lead to runtime errors or unpredictable behavior.
If a runtime error occurs, the program's accuracy will be considered ZERO.
If you are interested in the story behind this assignment, read Demystify Entropy for the motivation of encoding.