Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class kompr {
  2.  
  3.         public static String komp(String a) {
  4.                 String kompr = " ";
  5.                 char pre = 0;
  6.                 int counter = 0;
  7.  
  8.                 for (int i = 0; i < a.length(); i++) {
  9.                         if (pre == a.charAt(i)) {
  10.                                 counter += 1;
  11.  
  12.                         } else {
  13.                                 if (counter >= 3) {
  14.                                         kompr += counter +(char)pre;
  15.                                         counter = 1 ;
  16.                                 } else {
  17.                                         if (counter == 1)
  18.                                                 kompr += pre ;
  19.                                         if (counter == 2)
  20.                                                 kompr +=(char)pre + (char)pre;
  21.                                         counter = 1;
  22.                                         pre = a.charAt(i);
  23.                                 }
  24.                         }
  25.  
  26.                 }
  27.                 if (counter >= 3) {
  28.                         kompr += counter +(char)pre;
  29.                         counter = 1 ;
  30.                 } else {
  31.                         if (counter == 1)
  32.                                 kompr += pre ;
  33.                         if (counter == 2)
  34.                                 kompr += pre + pre;
  35.                         counter = 1;
  36.                 }
  37.  
  38.                 return kompr;
  39.  
  40.         }
  41.  
  42.         public static void main(String[] args) {
  43.                 System.out.println(komp("abbcccddddddddd"));
  44.         }
  45. }