-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher-- Decoding a Message.java
More file actions
54 lines (45 loc) · 1.21 KB
/
Cipher-- Decoding a Message.java
File metadata and controls
54 lines (45 loc) · 1.21 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
package placements;
/*
* In this program we have to decode the String and find the missing code. We have to break the string carefully.
* Like a String given is "31323435" , so if we see carefully we can find that 33 is missing.So Output = 33.
* Example 2:-
* "198199201"
* Output = 200
* */
import java.util.*;
public class Cipher {
static int decode(String x)
{
int answer=-1;
ArrayList<Integer> list = new ArrayList<>();
// when the length is even we break the string in a length of 2
if(x.length()%2==0)
{
for(int i =0;i<x.length();i=i+2)
{
list.add(Integer.parseInt(x.substring(i, i+2)));
}
}
// when the length is odd we break the string in a length of 3
else
{
for(int i =0;i<x.length();i=i+3)
{
list.add(Integer.parseInt(x.substring(i, i+3)));
}
}
System.out.println(list);
for(int i =0;i<list.size()-1;i++)
{
if(list.get(i+1)-list.get(i)>1)
answer = list.get(i+1)-1;
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message that needs to be decoded");
String x =sc.next();
System.out.println("The code after decoding is : "+ decode(x));
}
}