-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJava.java
55 lines (46 loc) · 1.67 KB
/
Java.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Chuck Norris */
/* Difficulty: Easy */
/* Date solved: 08.11.2018 */
/* */
/****************************************/
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//Read input.
Scanner scanner = new Scanner(System.in);
String MESSAGE = scanner.nextLine();
//Convert input message to binary representation.
String BINARY = "";
for (int i = 0; i < MESSAGE.length(); i++)
{
String charInBinary = Integer.toBinaryString((int)MESSAGE.charAt(i));
//Fill binary representation with zeroes to get 7 bit.
charInBinary = "0000000".substring(charInBinary.length()) + charInBinary;
BINARY += charInBinary;
}
//Convert binary representation in "Chuck Norris Code".
char lastChar = ' ';
String codedMessage = "";
String[] encodedBits = new String[] { " 00 0", " 0 0" };
for (int i = 0; i < BINARY.length(); i++)
{
if (BINARY.charAt(i) != lastChar)
{
lastChar = BINARY.charAt(i);
codedMessage += encodedBits[lastChar - '0'];
}
else
{
codedMessage += "0";
}
}
//Print encoded message.
System.out.println(codedMessage.substring(1));
}
}