Unofficial C Solution to Problem 1.5 in Cracking the Coding Interview (5th Edition)

20 Mar

Generally, this question requires us to do a length encoding of an string. For example, after compression, the string “aaaabb” should become “a4b2”. If the compression could not shorten the string, the program will leave original string unchanged.

I solve this question differently from the solution on crack5e.info. The crack5e.info is unavailable now. But as far as I remember, in the solution of crack5e.info, you have no way to get the expected length of compressed string. Thus you have to allocate as much memory as possibly needed. But in my solution, we have a choice to get the length of compressed string before really allocate and fill memory. The source code is as following.

One thought on “Unofficial C Solution to Problem 1.5 in Cracking the Coding Interview (5th Edition)

Leave a Reply

Your email address will not be published. Required fields are marked *