Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 else is just treated as a simple value. For this exercise you will compress one line of input at at time according to the following:

The newline characters are passed through uncompressed even when there are repeated blank lines. When a run of n>=3 repeated characters, c, is detected where c is NOT a digit, the run will be replaced with #nc. When a run of n>=3 repeated characters, c, is detected where c IS a digit, the run will be replaced with #n#c. The extra # is needed to avoid confusing the repeated digit c with the last digit of the run count. All other characters (i.e. runs of just 1 or 2 characters) are passed through unmodified. Assume the input does not contain the symbol '#'. This assumption can be eliminated. You might think about how you would do it.

Some examples:

abc compresses to abc

aaabcccc compresses to #3ab#4c

abc1233333333333333 compresses to abc12#14#3

Your encoder must include a public static method compress() that takes one parameter, a String, that is the line to be compressed. The method returns the compressed String.

Need help debugging my code with this problem.

import java.util.Scanner; public class Encoder { public static void main(String[] args) { String str; Scanner in = new Scanner(System.in); while(in.hasNext()) { str = in.nextLine(); //reads next line System.out.print(compress(str)); System.out.println(); } //runs decode of the user-inputted string in.close(); } public static String compress(String str) { String in = ""; String result = ""; int num = 0; for(int i=0; i if (str.charAt(i) == str.charAt(i + 1)) { while(str.charAt(i) == str.charAt(i + num) && i + num != str.length()-1){ num++; } if(num!=2 || i+num == str.length()-1 || str.charAt(str.length()-1) == str.charAt(i)) { num++; } else { result += str.charAt(str.length()-1); } if(num != 2 || Character.isLetter(str.charAt(i))) { if (Character.isLetter(str.charAt(i))) { in += "#" + num + str.charAt(i) + result; } else { in += "#" + num + "#" + str.charAt(i) + result; } } else if (str.charAt(i) == str.charAt(str.length() - 1)) { num += 1; str += "#" + num + str.charAt(i); } else { in += str.charAt(i); in += str.charAt(i); if (i + num == str.length() - 1) { in += str.charAt(str.length() - 1); } } i = i + num - 1; num = 0; result = ""; } else { in += str.charAt(i); if (i + num == str.length() - 2) { in += str.charAt(str.length() - 1); } } } return in; } } Thank you. Sorry for no comments.

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

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

More Books

Students also viewed these Databases questions

Question

What are the social tasks and challenges of adolescence?

Answered: 1 week ago

Question

Why do mergers and acquisitions have such an impact on employees?

Answered: 1 week ago

Question

2. Describe the functions of communication

Answered: 1 week ago