Question

Run-length encoding (RLE) is a simple

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

OutputMarkers□ Properties妃Servers膼Data Source Explorer Snippets 貝 terminated> Compression Java Application] C:AProgram Files Javalj

Program Compression.java

==============================================================

import java.util.Scanner;

public class Compression {

   // Method to compress string
   public static String compress(String original) {

       String compressedString = "";

       // Iterate over string character by character
       for (int i = 0; i < original.length(); i++) {

           char currentChar = original.charAt(i);

           int currentCharCount = 1;

           // Check for the next all characters which are equal to current
           // character
           while ((i + 1) < original.length() && original.charAt(i) == original.charAt(i + 1)) {

               currentCharCount++;
               i++;
           }

           // if currentCharCount >1 means compress applies
           if (currentCharCount > 1) {

               compressedString = compressedString + String.valueOf(currentCharCount) + String.valueOf(currentChar);
           } else {
               compressedString = compressedString + String.valueOf(currentChar);
           }

       }

       return compressedString;
   }

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       // Read original string from user
       System.out.println("Enter a string for compression: ");
       String original = sc.next();

       // Call compress function
       String compressedString = compress(original);

       // Display compressed string
       System.out.println("After applying RLE algorithm,this string is converted into:\n" + compressedString);

       // close stream
       sc.close();
   }

}

Add a comment
Know the answer?
Add Answer to:
Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data...
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
  • Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE...

    Write your code in the file StringRec.java. For this problem, the following restrictions apply: YOUR CODE MUST BE RECURSIVE. Do not use loops (while, do/while, or for). Do not declare any variables outside of a method. You may declare local variables inside a method. Complete the following method: public static String decompress(String compressedText): Decompress the input text, which has been compressed using the RLE algorithm (previous hw assignment): Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes...

  • 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 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...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

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