Ask Question

Write a method called compress that takes a string as input, compresses it using rle, and returns the compressed string. case matters - uppercase and lowercase characters should be considered distinct. you may assume that there are no digit characters in the input string. there are no other restrictions on the input - it may contain spaces or punctuation. there is no need to treat non-letter characters any differently from letters.

+1
Answers (1)
  1. 17 March, 11:33
    0
    public static String compress (String original) { StringBuilder compressed = new StringBuilder (); char letter = 0; int count = 1; for (int i = 0; i < original. length (); i++) { if (letter = = original. charAt (i)) { count = count + 1; } else { compressed = count!=1? compressed. append (count) : compressed; compressed. append (letter); letter = original. charAt (i); count = 1; } } compressed = count!=1? compressed. append (count) : compressed; compressed. append (letter); return compressed. toString (); }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a method called compress that takes a string as input, compresses it using rle, and returns the compressed string. case matters - ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers