Successfully reported this slideshow.
Your SlideShare is downloading. ×

Heres a simple strategy to encode a message- before each letter of the.docx

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 2 Ad

Heres a simple strategy to encode a message- before each letter of the.docx

Download to read offline

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
.

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
.

Advertisement
Advertisement

More Related Content

Similar to Heres a simple strategy to encode a message- before each letter of the.docx (20)

More from tpaula2 (20)

Advertisement

Recently uploaded (20)

Heres a simple strategy to encode a message- before each letter of the.docx

  1. 1. 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
  2. 2. Output: hey

×