Advertisement
FamiHug

Programming Challenge 1 - ZipString

Aug 2nd, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. /**
  2.  * @Author: FamiHug at FAMILUG
  3.  * @Version: 0.5  Tue Aug  2 15:01:54 ICT 2011
  4.  */
  5.  
  6. public class PRC01CompressString {
  7.     public static void main(String[] args)
  8.     {
  9.         ZipString zs = new ZipString();
  10.         zs.compressString("AAABBCAABDDAFF");
  11.     }
  12. }
  13.  
  14. class ZipString {
  15.     public void compressString(String input)
  16.     {
  17.         int charCounter = 1;
  18.         char currentChar = input.charAt(0);
  19.         char previousChar;
  20.  
  21.         for(int i = 1; i < input.length(); i++) {
  22.             previousChar = currentChar;
  23.             currentChar = input.charAt(i);
  24.             if(currentChar == previousChar) {
  25.                 charCounter++;
  26.             }
  27.             else {
  28.                 if(charCounter != 1)
  29.                     System.out.print(charCounter + "" + previousChar);
  30.                 else System.out.print(previousChar);
  31.  
  32.                 charCounter = 1;
  33.             }
  34.         }
  35.         System.out.println(charCounter + "" + currentChar);
  36.         System.out.println();
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement