THINK FIRST·CODE LATER

← All labs

Run-length compression with StringBuilder

Problem

Run-length encoding replaces repeated characters by one character and a count: aaabccdddd → a3bc2d4 (a count of 1 is not written).

Write RunLength with static String compress(String s) and static String expand(String s) (counts may have several digits: x12 means twelve x's), both using a StringBuilder. For each word of the input print

aaabccdddd -> a3bc2d4 (10 -> 7 chars) back: ok
  reversed: ddddccbaaa

back checks that expand(compress(w)) equals w. Use new StringBuilder(word).reverse() for the second line.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l13-4-data-1.txt

aaabccdddd
What your program must print for l13-4-data-1.txt
aaabccdddd -> a3bc2d4 (10 -> 7 chars) back: ok
  reversed: ddddccbaaa

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l13-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l13-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.