Question: Write a program rle that uses a simple method to compress strings. rle takes a single argument and looks for repeated characters. Each repeated sequence

Write a program rle that uses a simple method to compress strings. rle takes a single argument and looks for repeated characters. Each repeated sequence of a letter or punctuation mark is reduced to a single character plus an integer indicating the number of times it occurs. Thus, aaa becomes a3 and ab becomes a1b1.

If the compressed string is longer than the original string, rle must print the original string instead.

If the input string contains digits, rle MUST print error and nothing else.

Usage

$ ./rle aaaaaa

a6

$ ./rle aaabcccc..a

a3b1c4.2a1

$ ./rle aaabab aaabab

$ ./rle a1b2

error

Notes

Note that rle prints the original string if its compression method results in a larger string than the input. Thus, in the third example above, it prints aaabab (length 6) and not a3b1a1b1 (length 8).

You MUST NOT assume that input strings have a maximum length. You will need to allocate space to store the compressed string dynamically, based on the length of the input string. Given an input string containing n characters, what is the maximum number of characters it a printed output 3 string? Is it necessary for rle to compress the entire input string before determining that it should output the uncompressed string instead?

You MUST NOT assume any maximum number of times a character will be repeated. rle should work equally well given a sequence of 10 or 1000 As. Rather than write your own integer to string function, you can use sprintf or snprintf with an appropriate format string. Remember that these functions work perfectly well when given a pointer to the middle of an allocated char array, and will begin printing after that pointer. Both functions return the number of bytes witten, which you can use to advance your pointer. (Read the manual and do some experiments to make sure you account for terminator characters properly.)

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 Databases Questions!