-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryCode.java
More file actions
90 lines (72 loc) · 1.73 KB
/
BinaryCode.java
File metadata and controls
90 lines (72 loc) · 1.73 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package practice;
import java.util.Scanner;
public class BinaryCode {
String[] decode(String message){
char [] msg,p1Arr = null,p0Arr=null;
int [] p1,p0;
String[] bigBoss = {"",""};
String p00="",p11="";
msg=message.toCharArray();
int len;
len=msg.length;
//m=new int[len];
p1=new int[len];
p0=new int[len];
for(int count=0;count<msg.length;count++){
//m[count]=(int)msg[count];
if(count==0){
p1[count]=1;
p0[count]=0;
}
else if(count==1){
p1[count]=msg[(count-1)]-p1[(count-1)];
p0[count]=msg[(count-1)]-p0[(count-1)];
if(p1[count]>1 || p1[count]<0)
p11="NONE";
if(p0[count]>1 || p0[count]<0)
p00="NONE";
}else if(count!=(msg.length-1)){
p1[count]=msg[(count-2)]-(p1[(count-1)]+p1[(count-2)]);
p0[count]=msg[(count-2)]-(p0[(count-1)]+p0[(count-2)]);
if(p1[count]>1 || p1[count]<0)
p11="NONE";
if(p0[count]>1 || p0[count]<0)
p00="NONE";
}else{
if(msg[count]==(p1[count]+p1[count-1])){
;
}else{
p11="NONE";
}
if(msg[count]==(p0[count]+p0[count-1])){
;
}else{
p00="NONE";
}
}
}
for(int i=0;i<msg.length;i++){
if(!p00.equals("NONE")){
p0Arr[i]=(char) p0[i];
}
if(!p11.equals("NONE")){
p1Arr[i]=(char) p1[i];
}
}
if(!p00.equals("NONE"))
p00=new String(p0Arr);
if(!p11.equals("NONE"))
p11=new String(p0Arr);
bigBoss[0]=p00;
bigBoss[1]=p11;
return bigBoss;
}
public static void main (String args[]){
BinaryCode bc=new BinaryCode();
System.out.println("Input the values");
Scanner sc=new Scanner(System.in);
String message=sc.next();
String[] result=bc.decode(message);
System.out.println(result[0]+","+result[1]);
}
}