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, <space>, z are 65, 32 and 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 to 90 and
those of a..z are 97 to 122.
import java.io.*;
class Q2_2004
{
//Data Members
public String msg;
public String ans;
public String rev;
public int len;
public String
newans;
//Methods
//Default
Constructor
Q2_2004()
{
msg="";
ans="";
rev="";
len=0;
newans="";
}
//Function to
input the encoded message
public void
Input()
throws IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int i,len1;
System.out.println("Enter the encoded message");
msg=br.readLine();
len1=msg.length();
for(i=(len1-1);i>=0;i--)
rev+=msg.charAt(i);
len=rev.length();
}
//Function to
extract the digits from the reverse number and decode it
public void
Decode()
throws IOException
{
Input();
int
lwrnum[]={97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
char
lwrcas[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int
uprnum[]={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};
char
uprcas[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int i,j;
int digit;
int number=0;
for(i=0;i<len;i++)
{
digit=Integer.parseInt(rev.substring(i,i+1));
number=number*10+digit;
if(number>=65 && number
<=90)
{
for(j=0;j<26;j++)
{
if(number==uprnum[j])
ans+=uprcas[j];
}//j
number=0;
}//if
else
if(number>=97 && number <=122)
{
for(j=0;j<26;j++)
{
if(number==lwrnum[j])
ans+=lwrcas[j];
}//j
number=0;
}//if
else
if(number==32)
{
ans+=" ";
number=0;
}
}//i
ans="
"+ans;
ans+="
";
len=ans.length();
char ltr;
for(i=0;i<len;i++)
{
ltr=ans.charAt(i);
if(ltr!='
')
newans+=ltr;
else
if(ltr=='
')
{
newans+=ltr;
ltr=ans.charAt(i+1);
for(j=0;j<26;j++)
{
if(ltr==lwrcas[j])
ltr=uprcas[j];
}
newans+=ltr;
i++;
}//if
}
}
//Function to
Display the string
public void Display()
throws IOException
{
Decode();
System.out.print("The decoded message is :: "+newans);
}
}
import java.io.*;
class Q2_2004main
{
public void main()
throws IOException
{
Q2_2004 SM=new
Q2_2004();
SM.Display();
}
}
Sample Output
Enter the encoded message
2312179862310199501872379231018117927
The decoded message is ::
Have A Nice Day
No comments:
Post a Comment