class SimpleCode { public static int[] encode (String original) { BitStream bits = new BitStream(); // Traverse the characters of the original. for (int index = 0; index < original.length(); index++) { char symbol = original.charAt(index); if (symbol == 'e') { bits.addBits(0, 1); } else { bits.addBits(1, 1); bits.addBits(symbol, 8); } } // Encode the end-of-message. bits.addBits(1, 1); bits.addBits(0, 1); return bits.toIntArray(); } public static String decode (int[] encoded) { BitStream bits = new BitStream(encoded); // Traverse the bit stream until the end-of-message is // encountered. boolean done = false; String s = new String(); while (!done) { // Read the next bit. int bit = bits.readBits(1); if (bit == 0) { s = s + 'e'; } else { char symbol = (char)bits.readBits(8); if (symbol == '\0') { done = true; } else { s = s + symbol; } } } return s; } public static void main (String[] args) { System.out.print("Enter a string: "); String s = User.readLine(); int[] encoded = encode(s); String decoded = decode(encoded); System.out.println(decoded); } }