Question: finish part B Part A . [ 4 mark ] Header Encoding Write a program that reads a line of printable characters and encodes with

finish part B Part A.[4 mark] Header Encoding
Write a program that reads a line of printable characters and encodes with a sequence of keys with each key is a binary string of '0's and '1's. The sequence of keys starts with one key with length 1, followed by three keys with length 2, seven keys of length 3, fifteen keys of length 4, etc. The keys of the same length are in sequence of its binary value but not with all '1's.
The encoding scheme is as follows.
Assume the input header is: \(\mathbf{n}(\mathbf{X}+\# \mathbf{\$ 90\"?...}\)
key string:
To verify the correctness of the encoding, write a function to read in a character and print the key(s) of that character.
Notes:
1. You may not know the length of the header in advance.
2. The maximum length of the key string is seven. That is, the longest key string is "1111110". Hint: A character string should have a '\(\backslash 0\)' at the end of the string.
3. The header contains only printable characters including "space", i.e. any characters in the ASCII Table with values from 32 to 126.
4. We assume the input is valid, i.e., no need to check the correctness of input.
5. The header may have repeated characters that lead to different keys.
Sample Input and Output
Example 1
Enter Header:
n(X+\# \$90)"?
Enter character:
\$
010
Example 2
Enter Header:
n(X+\# \$90)"?n
Enter character:
Example 3 Part B.[4 marks] Message Decoding
Extend your program in Part A to decode a message based on the keys generated in Part A. The encoded message is a sequence of binary digits. The message should be decoded in segments. Each segment starts with the first 3 digits to represent the length of keys to be decoded in the subsequent digits. The end of each segment is signified by a sequence of '1's of that length.
With the example header in Part A, the segment would be decoded as follows.
The message may have multiple segments and ends with the binary string "000".
Example 1: message 010010011(1 segment)\(\rightarrow \) decoded message \(\mathbf{X (}\)
Example 2: message 1000100111(2 segments)\(\rightarrow \) decoded message \#\$9n
Your program should read in the header and an encoded message; then decode the message and output the decoded message.
Notes:
1. Refer to the Notes of Part A.
2. The encoded message is assumed to be in one line.
3. We assume the input is valid.
Sample Input and Output
Example 1
Enter Header:
n(X+\# \$90\"?
Enter message:
\[
\begin{array}{l}
011000001011111001010111101110100111000\\
\text {\# 9n"X }
\end{array}
\]
Example 2
Enter Header:
:-+ IkC
Enter message:
\[
\begin{array}{l}
011010111010010111000\\
\text { C++}
\end{array}
\]
Example 3
Enter Header:
=>?@A pqrstuv !"\#\$EFSTCabgh673rs-./01cde92ieo83r
Enter message:
100101110011111101011111010001010010011111101100111110110000111110111011111000010
110000101110111110110101011011111110011001111011110111100001011111010101110010111
11100000011111011010111111011101111101100011111110000111111000
CS2310 is a great course!
Example 4
Enter Header:
giOAstd!2..mt-e+n
Enter message:
0101011011000000111010001100101100010100000011010100011111011100111000
Assignment2
finish part B Part A . [ 4 mark ] Header Encoding

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!