Certainly! Below is a C program that accomplishes your request:
- Creates a long string of text.
- Converts it into numbers (ASCII codes).
- Performs mathematical operations to compress it (using Run-Length Encoding).
- Writes the sizes of both the original and compressed data.
- Decompresses it and matches it with the actual text.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function prototypes
unsigned char* compress(const unsigned char *input, size_t length, size_t *compressed_length);
unsigned char* decompress(const unsigned char *input, size_t length, size_t *decompressed_length);
int main() {
// Original text
const char *text = "AAABBBBCCDAA"; // Replace with a longer string or read from a file
size_t text_length = strlen(text);
printf("Original Text: %s\n", text);
printf("Original Size: %zu bytes\n", text_length);
// Convert text to ASCII codes (numbers)
unsigned char *ascii_codes = (unsigned char *)malloc(text_length * sizeof(unsigned char));
for (size_t i = 0; i < text_length; i++) {
ascii_codes[i] = (unsigned char)text[i];
}
// Compress the ASCII codes
size_t compressed_length = 0;
unsigned char *compressed_data = compress(ascii_codes, text_length, &compressed_length);
printf("Compressed Size: %zu bytes\n", compressed_length);
// Decompress the data
size_t decompressed_length = 0;
unsigned char *decompressed_data = decompress(compressed_data, compressed_length, &decompressed_length);
// Convert decompressed ASCII codes back to characters
char *decompressed_text = (char *)malloc(decompressed_length + 1);
for (size_t i = 0; i < decompressed_length; i++) {
decompressed_text[i] = (char)decompressed_data[i];
}
decompressed_text[decompressed_length] = '\0';
printf("Decompressed Text: %s\n", decompressed_text);
// Verify that the original and decompressed texts are the same
if (strcmp(text, decompressed_text) == 0) {
printf("Success: Decompressed text matches the original!\n");
} else {
printf("Error: Decompressed text does not match the original.\n");
}
// Free allocated memory
free(ascii_codes);
free(compressed_data);
free(decompressed_data);
free(decompressed_text);
return 0;
}
// Simple Run-Length Encoding compression
unsigned char* compress(const unsigned char *input, size_t length, size_t *compressed_length) {
unsigned char *compressed = (unsigned char *)malloc(length * 2); // Worst case
size_t index = 0;
for (size_t i = 0; i < length; ) {
unsigned char current_char = input[i];
size_t count = 1;
while ((i + count < length) && (input[i + count] == current_char) && (count < 255)) {
count++;
}
compressed[index++] = count;
compressed[index++] = current_char;
i += count;
}
*compressed_length = index;
return compressed;
}
// Decompression of Run-Length Encoded data
unsigned char* decompress(const unsigned char *input, size_t length, size_t *decompressed_length) {
size_t estimated_size = length * 255; // Maximum possible size
unsigned char *decompressed = (unsigned char *)malloc(estimated_size);
size_t index = 0;
for (size_t i = 0; i < length; i += 2) {
unsigned char count = input[i];
unsigned char value = input[i + 1];
for (unsigned char j = 0; j < count; j++) {
decompressed[index++] = value;
}
}
*decompressed_length = index;
return decompressed;
}
Explanation:
-
Compression Algorithm: The program uses a simple Run-Length Encoding (RLE) algorithm. It compresses sequences of repeated characters by storing the count and the character.
-
Conversion to Numbers: The text is converted into an array of unsigned char (ASCII codes), effectively representing each character as its numeric ASCII value.
-
Sizes: The program prints the size of the original text and the compressed data.
-
Decompression: The compressed data is decompressed back to the original form and verified for accuracy.
Sample Output:
Original Text: AAABBBBCCDAA
Original Size: 12 bytes
Compressed Size: 10 bytes
Decompressed Text: AAABBBBCCDAA
Success: Decompressed text matches the original!
Instructions to Test the Program:
-
Replace the Text: You can replace the text variable with a longer string or read from a file to test with more data.
// Example of reading from a file
FILE *file = fopen("input.txt", "r");
fseek(file, 0, SEEK_END);
size_t text_length = ftell(file);
fseek(file, 0, SEEK_SET);
char *text = malloc(text_length + 1);
fread(text, 1, text_length, file);
text[text_length] = '\0';
fclose(file);
-
Compile the Program: Save the code to a file named compress.c and compile it using:
gcc -o compress compress.c
-
Run the Program:
./compress
Notes:
- The RLE compression is most effective for texts with many repeated characters.
- The compressed size may not always be smaller than the original size, especially for texts without many repetitions.
- The program assumes that no run length will exceed 255 (maximum value for an unsigned char). For longer runs, modifications are needed.
Understanding the Mathematical Compression:
-
Conversion to Numbers: Each character in the text is converted to its ASCII numeric value.
-
Mathematical Operation: The compression uses counts (numbers) and the character codes, effectively reducing sequences of the same character.
-
Compression Logic:
- For a sequence like
AAAAA, instead of storing 65 65 65 65 65 (ASCII codes for 'A'), it stores 5 65, representing that 'A' occurs 5 times consecutively.
- This reduces the amount of data needed to represent repeated characters.
Decompression and Verification:
- The decompression function reverses the process, reconstructing the original sequence using the counts and character codes.
- The program compares the decompressed text with the original to ensure they match.
Feel free to modify the code and explore further! If you have any questions or need additional assistance, don't hesitate to ask.