Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @Author: FamiHug at FAMILUG
- * @Version: 0.5 Tue Aug 2 15:01:54 ICT 2011
- */
- public class PRC01CompressString {
- public static void main(String[] args)
- {
- ZipString zs = new ZipString();
- zs.compressString("AAABBCAABDDAFF");
- }
- }
- class ZipString {
- public void compressString(String input)
- {
- int charCounter = 1;
- char currentChar = input.charAt(0);
- char previousChar;
- for(int i = 1; i < input.length(); i++) {
- previousChar = currentChar;
- currentChar = input.charAt(i);
- if(currentChar == previousChar) {
- charCounter++;
- }
- else {
- if(charCounter != 1)
- System.out.print(charCounter + "" + previousChar);
- else System.out.print(previousChar);
- charCounter = 1;
- }
- }
- System.out.println(charCounter + "" + currentChar);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement