Here’s a simple strategy to encode a message: before each letter of the message, add a number and series of letters. The number should correspond to the number of letters that will precede the message\'s actual, meaningful letter. For example, the word “hey†could be coded with “0h2abe1zyâ€. To read the message, you would: skip 0, find the ‘h’ skip 2 (‘a’ and ‘b’), find ‘e’ skip 1 (‘z’), find ‘y’ Write a function called “decodeâ€, which takes a string in this code format and returns the decoded word. You may assume that coded strings are always legally encoded with this system.
Solution
import java.util.*;
public class Decode {
public static void main(String ar[])
{
String code,decode=\"\";
Scanner s=new Scanner(System.in);
System.out.print(\"enter any one code \");
code=s.next();
int l=code.length();
for(int i=0;i<l;i++)
{
if(Character.isDigit(code.charAt(i))==true)
{
String c=\"\"+code.charAt(i);
int n=Integer.parseInt(c);
decode+=code.charAt(i+1+n);
}
}
System.out.print(decode);
}
}
Input :
0h2abe1zy
Output:
hey
.