Question: #include #include void generateKeys ( char keys [ ] [ 8 ] , int& keyCount ) { int length = 1 ; keyCount = 0

#include
#include
void generateKeys(char keys[][8], int& keyCount){
int length =1;
keyCount =0;
// Generate keys of lengths from 1 to 7
while (keyCount 127 && length =7){
int numKeys =(1 length)-1; // Number of keys for the given length
for (int i =1; i = numKeys; ++i){
for (int j = length -1; j >=0; --j){
keys[keyCount][j]=(i & (1(length -1- j)))?'1' : '0';
}
keys[keyCount][length]='\0'; // Null-terminate the string
++keyCount;
if (keyCount >=127){
return; // Stop if we reach the maximum key count
}
}
++length; // Increase length for the next round
}
}
void encodeHeader(const char* header, char keys[][8], char encoding[][8], int keyCount){
int length = std::strlen(header);
for (int i =0; i length && i keyCount; ++i){
strcpy(encoding[i], keys[i]);
}
}
int binaryToDecimal(const char* binary){
int decimal =0;
for (int i =0; binary[i]!='\0'; ++i){
decimal =(decimal 1)+(binary[i]-'0');
}
return decimal;
}
void decodeMessage(const char* encodedMessage, const char* header, char keys[][8], int keyCount){
const char* segment = encodedMessage;
while (*segment){
if (strlen(segment)3) break; // Not enough characters for length
// Read the length of the key from the first 3 bits
char lengthStr[4]={ segment[0], segment[1], segment[2],'\0'};
int keyLength = binaryToDecimal(lengthStr);
segment +=3; // Move past the length bits
// Look for the end of the segment marked by '0's
while (strncmp(segment, std::string(keyLength,'0').c_str(), keyLength)!=0){
char key[8];
strncpy(key, segment, keyLength);
key[keyLength]='\0'; // Null-terminate the key
// Compare the key against the generated keys
for (int i =0; i keyCount; ++i){
if (strcmp(key, keys[i])==0){
std::cout header[i]; // Print the corresponding character
break; // Stop after finding the match
}
}
segment += keyLength; // Move forward in the segment
}
segment += keyLength; // Skip over the '0's that mark the end of this segment
}
std::cout std::endl; // Newline after decoding
}
int main(){
char header[257];
std::cout "Enter Header:" std::endl;
std::cin.getline(header,257);
char keys[127][8];
char encoding[256][8];
int keyCount =0;
generateKeys(keys, keyCount); // Generate binary keys
encodeHeader(header, keys, encoding, keyCount); // Encode the header
std::cout "Enter code:" std::endl;
char encodedMessage[1000];
std::cin.getline(encodedMessage,1000); // Read the encoded message
decodeMessage(encodedMessage, header, keys, keyCount); // Decode and print the message
return 0;
}
This is my code and it give the wrong output when input is (r0]~-+mI@{#Ongf@foX^kF8ruS?vd1w(qt|N&@P5%BLF2@Yr+3TO\^U*}pYx)mT?N=m[Fv=2BZT%T>HGH'zlAotW}T+Lt{m)*f[!*)^khrb@4#%Jd
#include #include void generateKeys ( char keys [

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!