Write your code in the file StringRec.java. For this problem, the following rest
ID: 3578182 • Letter: W
Question
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 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 the sequence
the repeating character
If a character does not repeat, it should be left alone.
For example, consider the following string:
After applying the RLE algorithm, this string is converted into:
In the compressed string, "9w" represents a sequence of 9 consecutive lowercase "w" characters. "5e" represents 5 consecutive lowercase "e" characters, etc.
Explanation / Answer
//importing packages
import java.util.*;
import java.io.*;
//Declaring class name as StringRec
public class StringRec{
//main method start
public static void main(String[] args) {
//Asking user for enter text compressing text
System.out.println("What text do you want to decompress?");
//reading input and storing into text variable
String text = IO.readString();
//calling decompress method for printing o/p
System.out.println(decompress(text));
}
// decompress method as static takes text string type argument and returns String variable
public static String decompress(String text) {
//if input length is less than 1 then return text
if (text.length()<=1){
return text;
}
//declaring local variables
String firstcharacter="";
String rest="";
char c = text.charAt(0);
if (Character.isLetter(c) == true) {
firstcharacter = text.substring(0,1);
rest = text.substring(1);
return firstcharacter + decompress(rest);
} else {
firstcharacter = text.substring(1,2);
rest = text.substring(2);
int x = text.charAt(0)-'0';
char y = text.charAt(1);
String tst = "";
//declaring local variable in else block
int i = 0;
//calling append method for recursively appending reaming string
append(String tst);
}//else
return tst + decompress(rest);
} //decompress()
//append static method takes tst string type argument and returns String variable
public static String append(String tst) {
if(i >= x) {
i = 0;
return tst;
}
else return append(tst + y);
}//append()
}//main
}//class
i/p: qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
o/p: q9w5e2rt5y4qw2Er3T
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.