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 hightlight the space).
The
ASCII values of A, <space>, 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.
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: 2312179862310199501872379231018117927
OUTPUT:
The
Decoded Message: Have a Nice Day
6532122
Now the integer values 6,5,3,2,1,2,2 will be stored in some array;
To get the integer value of the character '6' (say) we need to subtract 48 from it.
since 54 is the ascii value of '6'. thus 54-48 = 6.
This has been achived by the following code
int j = 0;
for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position from the String msg in
} // in reverse order
}
Now the idea is to extrac the ascii values from the array and print the
corresponding character
For example, we need to extrac 65, 32 and 122 from the array.
For this note that the ascii values of characters ranges from 65 to 90
and 97 to 122 and one extra value of 32 for the space.
So we will take the first 3 values from the integer, 6 5 and 2 and convert
it into decimal number by 6*10*10+5*10+2=652
but this is outside our range, so we need to take only 6 and 5 and convert it
to get 65. then print the corresponding character. For this we have used the code
while ( i < Decryptmsg.length() ) {
if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
else
tmp = codeValues[i]*10+codeValues[i+1];
if (tmp >= 97 && tmp <=122) {
System.out.print((char)tmp); // prints the character for the corresponding ascii
i +=3;
}
else {
tmp = codeValues[i]*10+codeValues[i+1];
System.out.print((char)tmp); // prints the character for the corresponding ascii
if(tmp==32)
System.out.print("");
i +=2;
}
}
Below is the link of solved ISC Computer Science Practical Paper year wise.
import java.util.*;
import java.io.*;
public class ISC2004Q1 {
public static void main(String[] args) throws IOException
{
String msg;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter the sentence (Encoded Message):");
msg = br.readLine();
Decrypt ob = new Decrypt(msg);
ob.reverseString();
System.out.println("OUTPUT The Decoded Message: ");
ob.decryptCode();
}
}
class Decrypt {
Decrypt(String msg) {
Decryptmsg = msg;
codeValues = new int[Decryptmsg.length()];
}
public void reverseString() {
int j = 0;
for ( int i = Decryptmsg.length()-1; i >= 0; i--) {
codeValues[j++] = Decryptmsg.charAt(i)-48; // for extracting the integer value at the i-th position
from the String msg in
} // in reverse order
}
public void decryptCode() {
int tmp,i=0;
while ( i < Decryptmsg.length() ) {
if (i < Decryptmsg.length() && i+1 < Decryptmsg.length() && i+2 < Decryptmsg.length())
tmp = codeValues[i]*10*10+codeValues[i+1]*10+codeValues[i+2];
else
tmp = codeValues[i]*10+codeValues[i+1];
if (tmp >= 97 && tmp <=122) {
System.out.print((char)tmp); // prints the character for the
corresponding ascii
i +=3;
}
else {
tmp = codeValues[i]*10+codeValues[i+1];
System.out.print((char)tmp); // prints the character for the
corresponding ascii
if(tmp==32)
System.out.print("");
i +=2;
}
}
}
private String Decryptmsg;
private int[] codeValues;
}