Using the Java language, have the function RunLength( str ) take the str parameter being passed and return a compressed version of the string using the Run-length encoding algorithm. This algorithm works by taking the occurrence of each repeating character and outputting that number along with a single character of the repeating sequence. For example: "wwwggopp" would return 3w2g1o2p . The string will not contain any numbers, punctuation, or symbols. import java.util.*; import java.io.*; class Function { String RunLength(String str) { String resposta = ""; int contador = 1; char c = str.charAt(0); for (int i = 1; i < str.length(); i++) { if (str.charAt(i) == c) { contador++; } else { resposta += contador+""+c; c = str.charAt(i); contador = 1; } } resposta += contador+""+c; return resposta; } public static void main (String[]
Comments
Post a Comment