Question

Run-length encoding is a relatively simple technique for compressing data; if a particular value (or substring)...

Run-length encoding is a relatively simple technique for compressing data; if a particular value (or substring) appears multiple times in a row, it is only represented once, followed by an integer indicating the number of times it should be repeated. For example, the string "AAAAA" can be compressed to "A5" (5 occurrences of 'A'), saving three characters in the process.

Define a Java method named expand() that takes a single String argument. You may assume that this String is run-length encoded and contains an even number of characters (each pair of characters consists of a printing character followed by a single digit representing its repetition count). Your method should return a new String that contains the expanded (or decoded) version of its argument. For example, calling expand(“a3b7a2c4") would return the String “aaabbbbbbbaacccc”.

Complete this problem by writing a complete Java program that prompts the user to enter a run-length encoded string, passes it to your expand() method, and displays the decoded result.

Hint: Java’s Integer.parseInt() method may be helpful here if you want to avoid performing character arithmetic.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class CompressExpandString {

/**

* method to encode the string

*

* @param s

* @return

*/

public static String encoding(String s) {

int count = 1;

int mark = 0;

StringBuilder builder = new StringBuilder();

for (int i = 1; i < s.length(); i++) {

if (s.charAt(i) == s.charAt(i - 1) && i < s.length() - 1) {

count++;

} else if (i == s.length() - 1 && s.charAt(i) == s.charAt(i - 1)) {

count++;

builder.append(s.charAt(mark));

builder.append(count);

count = 1;

mark = i;

} else {

builder.append(s.charAt(mark));

builder.append(count);

count = 1;

mark = i;

}

}

return builder.toString();

}

/**

* method to expand the string

*

* @param input

* @return

*/

public static String expand(String input) {

StringBuilder result = new StringBuilder();

Character currentCharacter;

int times;

for (int i = 0; i < input.length(); i++) {

currentCharacter = input.charAt(i);

if (Character.isDigit(currentCharacter)) {

times = Integer.parseInt(currentCharacter + "");

for (int j = 0; j < times; j++) {

result.append(input.charAt(i - 1));

}

}

}

return result.toString();

}

public static void main(String z[]) {

String input = "aaabbbbbbbaacccc";

String compressed = encoding(input);

String expand = expand(compressed);

System.out.println("Input : " + input);

System.out.println("Encodded : " + compressed);

System.out.println("Expanded : " + expand);

}

}

OUTPUT:

Input : aaabbbbbbbaacccc
Encodded : a3b7a2c4
Expanded : aaabbbbbbbaacccc

Add a comment
Know the answer?
Add Answer to:
Run-length encoding is a relatively simple technique for compressing data; if a particular value (or substring)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data...

    Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of: the number of characters in...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined...

    Write a function decompressed(count_tuples) that takes a list of counts of '0's and '1's as defined above and returns the expanded (un-compressed) string. You may assume that the first value of count_tuples has a count that is greater than or equal to zero and all other counts are greater than zero. Comments to show how to figure this out would be greatly appreciated A data file in which particular characters often occur multiple times in a row can be compressed...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do...

    Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do a rough comparative test to see how long some of the operations take relative to the predefined StringBuilder class and the predefined String class. Details: You will complete a simple simulation to compare the times required for three operations: append(char c) , insert(int loc, char c) and delete(int start, int stop) These methods are defined in the StringBuilder and MyStringBuilder classes, but you will...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT