Decode Intercepted messages...
The computer department of the Agency of International Espionage is trying to decode Intercepted messages. The agency’s spies have determined that the enemy encodes Messages by first converting all characters to their ASCII values and then reversing the string.
For example, consider A_z (the underscore is just to highlight the space). The ASCII values of A,, z are 65, 32, 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it. The coded message will not exceed 200 characters. It will contain only alphabets (A …… Z, and a …….z) and spaces.
ASCII values of A ……Z are 65 ….. 90 and those of a……z are 97 …..122.
Test your Program for the following data and some random data.
SAMPLE DATA:
INPUT:
Encoded Message:
2 3 1 2 1 7 9 8 6 2 3 1 0 1 9 9 5 0 1 8 7 2 3 7 9 2 3 1 0 1 8 1 1 7 9 2 7
OUTPUT:
THE DECODED MESSAGE: Have a Nice Day
* * *
INPUT:
Encoded Message:
23 5 1 1 0 1 1 5 0 1 7 8 2 3 5 1 1 1 2 1 7 9 9 1 1 8 0 1 5 6 2 3 4 0 1 6 1 1 7 1 1 4 1 1 4 8
OUTPUT:
THE DECODED MESSAGE: Truth Always Wins
* * *
(* should be capital letters)
Solution:-
import java.util.*;
class ComputerDepartment
{
String encoded,decoded="";
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Welcome To The Computer Department");
System.out.println("Enter a Encoded Message");
encoded=sc.next();
}
void reverse()//Function Will reverse the inputed String
{
char ch;
String str=new String();
for(int i=0;i<encoded.length();i++)
{
ch=encoded.charAt(i);
str=ch+str;
}
encoded=str;
}
void decode()//Function will convert number into Charcater
{
String str=new String(),x=new String();
char ch1;
char ch;
int code=0;
for(int i=0;i<encoded.length();i++)
{
ch=encoded.charAt(i);
str=str+ch;
if(str.length()==2)
{
code=Integer.parseInt(str);
if(code>=65&&code<=90||code>=97&&code<=100)
{
ch1=(char)(code);
x=x+ch1;
str="";
}
if (code==32)
{
ch1=' ';
x=x+ch1;
str="";
}
}
else if(str.length()==3)
{
code=Integer.parseInt(str);
if(code>=100&&code<=122)
{
ch1=(char)(code);
x=x+ch1;
str="";
}
}
}
decoded=decoded+x;
}
void display()
{
System.out.println("Decoded Message\n"+decoded);
}
public static void main(String args[])
{
ComputerDepartment obj=new ComputerDepartment();
obj.input();
obj.reverse();
obj.decode();
obj.display();
}
}
No comments:
Post a Comment
any problem in any program comment:-