
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 0.80 KB | hits: 11 | expires: Never
public class kompr {
public static String komp(String a) {
String kompr = " ";
char pre = 0;
int counter = 0;
for (int i = 0; i < a.length(); i++) {
if (pre == a.charAt(i)) {
counter += 1;
} else {
if (counter >= 3) {
kompr += counter +(char)pre;
counter = 1 ;
} else {
if (counter == 1)
kompr += pre ;
if (counter == 2)
kompr +=(char)pre + (char)pre;
counter = 1;
pre = a.charAt(i);
}
}
}
if (counter >= 3) {
kompr += counter +(char)pre;
counter = 1 ;
} else {
if (counter == 1)
kompr += pre ;
if (counter == 2)
kompr += pre + pre;
counter = 1;
}
return kompr;
}
public static void main(String[] args) {
System.out.println(komp("abbcccddddddddd"));
}
}