Advertisement
FamiHug

Programming Challenge 1 - StringZipper

Aug 2nd, 2011
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. /**
  2.  * @Author: FamiHug at FAMILUG
  3.  * @Version: 1.0 Tue Aug  2 23:53:23 ICT 2011
  4.  */
  5.  
  6. public class PRC01CompressString {
  7.     public static void main(String[] args)
  8.     {
  9.         StringZipper sz = new StringZipper();
  10.  
  11.         System.out.println("Programming Challenger 01 - FAMILUG\u2122 \n");
  12.  
  13.         String input = "AAABBCAABDDAFF";
  14.         System.out.println("Input: " + input + "\n");
  15.  
  16.         String compressed = sz.compress(input);
  17.         System.out.println("Compressed: " + compressed);
  18.  
  19.         String extracted = sz.extract(compressed);
  20.         System.out.println("Extracted: " + extracted);
  21.     }
  22. }
  23.  
  24. class StringZipper {
  25.     public String compress(String input)
  26.     {
  27.         int charCounter = 1;
  28.         char currentChar = input.charAt(0);
  29.         char previousChar;
  30.         StringBuffer compressedString = new StringBuffer();
  31.  
  32.         for(int i = 1; i < input.length(); i++) {
  33.             previousChar = currentChar;
  34.             currentChar = input.charAt(i);
  35.             if(currentChar == previousChar) {
  36.                 charCounter++;
  37.             }
  38.             else {
  39.                 if(charCounter != 1) {
  40.                     //System.out.print(charCounter + "" + previousChar);
  41.                     compressedString.append(charCounter);
  42.                     compressedString.append(previousChar);
  43.                 }
  44.                 else compressedString.append(previousChar);
  45.  
  46.                 charCounter = 1;
  47.             }
  48.         }
  49.         compressedString.append(charCounter);
  50.         compressedString.append(currentChar);
  51.         compressedString.append("\n");
  52.         //System.out.println(charCounter + "" + currentChar);
  53.         return compressedString.toString();
  54.     }
  55.  
  56.     public String extract(String compressedString) {
  57.         int loopTimes = 0;
  58.         int currentChar;
  59.         StringBuffer extracted = new StringBuffer();
  60.  
  61.         for(int i = 0; i < compressedString.length(); i++)
  62.         {
  63.             if(Character.isDigit(compressedString.charAt(i))) loopTimes = loopTimes * 10 + Character.digit(compressedString.charAt(i), 10);
  64.             else
  65.             {
  66.                 if(loopTimes == 0) loopTimes = 1;
  67.                 for(int j = 0; j < loopTimes; j++) {
  68.                     //System.out.print(compressedString.charAt(i));
  69.                     extracted.append(compressedString.charAt(i));
  70.                 }
  71.                 loopTimes = 0;
  72.             }
  73.         }
  74.         //System.out.println();
  75.         extracted.append("\n");
  76.         return extracted.toString();
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement