Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions