Question: #include #include void generateKeys ( char keys [ ] [ 8 ] , int& keyCount ) { int length = 1 ; keyCount = 0
#include
#include
void generateKeyschar keys int& keyCount
int length ;
keyCount ;
Generate keys of lengths from to
while keyCount && length
int numKeys length; Number of keys for the given length
for int i ; i numKeys; i
for int j length ; j ; j
keyskeyCountji & length j : ;
keyskeyCountlength; Nullterminate the string
keyCount;
if keyCount
return; Stop if we reach the maximum key count
length; Increase length for the next round
void encodeHeaderconst char header, char keys char encoding int keyCount
int length std::strlenheader;
for int i ; i length && i keyCount; i
strcpyencodingi keysi;
int binaryToDecimalconst char binary
int decimal ;
for int i ; binaryi; i
decimal decimal binaryi;
return decimal;
void decodeMessageconst char encodedMessage, const char header, char keys int keyCount
const char segment encodedMessage;
while segment
if strlensegment break; Not enough characters for length
Read the length of the key from the first bits
char lengthStr segment segment segment;
int keyLength binaryToDecimallengthStr;
segment ; Move past the length bits
Look for the end of the segment marked by s
while strncmpsegment std::stringkeyLengthcstr keyLength
char key;
strncpykey segment, keyLength;
keykeyLength; Nullterminate the key
Compare the key against the generated keys
for int i ; i keyCount; i
if strcmpkey keysi
std::cout headeri; Print the corresponding character
break; Stop after finding the match
segment keyLength; Move forward in the segment
segment keyLength; Skip over the s that mark the end of this segment
std::cout std::endl; Newline after decoding
int main
char header;
std::cout "Enter Header:" std::endl;
std::cin.getlineheader;
char keys;
char encoding;
int keyCount ;
generateKeyskeys keyCount; Generate binary keys
encodeHeaderheader keys, encoding, keyCount; Encode the header
std::cout "Enter code:" std::endl;
char encodedMessage;
std::cin.getlineencodedMessage; Read the encoded message
decodeMessageencodedMessage header, keys, keyCount; Decode and print the message
return ;
This is my code and it give the wrong output when input is r~mI@#Ongf@foXkFruS?vdwqtN&@PBLF@YrTOUpYxmTNmFvBZTTHGH'zlAotWTLtmfkhrb@#Jd
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
