Sei sulla pagina 1di 4

The Computer Department of the Agency of International Episonage is trying to

decode intercepted messages. The agency's spies have determined that the enemy
encodes messages by first converting all characters all characters to their ASCII
values and then reversing the string.
Write a program 'DECODE' which reads a coded message and decodes it. The
coded message will not exceed 200 characters. It will conatin only alphabets and
spaces.
ALGORITHM
Main() Method
STEP 1: START
STEP 2: Call the function input(). Take the code input.
STEP 3: Call the function numbers(). Store the different numbers in array.
STEP 4: Call the function construct(). Construct the decoded message.
STEP 5: Call the function display(). Display the decoded message.
STEP 6: END
Input() Method
STEP 1: START
STEP 2: Store the entered code in str.
STEP 3: END
Numbers() Method
STEP 1: START
STEP 2: Create String Buffer for str and reverse the code.
STEP 3: Store the length of string in len.
STEP 4: Initialize int a=0. Run while loop until a<len and repeat STEP 5 and 6.
STEP 5: Store the ASCII value for the character equivalent of the digit in int b.
STEP 6: Store this in integer array arr. Check the ASCII value for a valid alphabet.
a) If the character is valid, increment the array index.
b) Else in the next run of loop, increase the face value of the digit and add the
next digit to it until a valid ASCII character equivalent is obatined.

STEP 7: END
Construct Method()
STEP 1: START
STEP 2: Convert the array of integers formed into the equivalent ASCII characters
to form the Decoded message in Str3 using for loop.
STEP 3: END
Display Method()
STEP 1: START
STEP 2: Display Str3 (Decoded Meassge)
STEP 3: END

import java.util.*;
public class Decode
{
String Str, Str3="";
int arr[]= new int [200];
int k=0;
void main()

//Declaring instance Variables


//The Main Function

{
input();
numbers();
construct();
display();
}

void input()
{

//Function for input of data

Scanner scan= new Scanner(System.in);


System.out.println("AGENCY OF INTERNATIONAL EPISONAGE");
System.out.println();
System.out.println("Please Enter code: ");
System.out.println();
Str= scan.nextLine();
}

void numbers()
{
StringBuffer Str2= new StringBuffer(Str);
Str2.reverse();
Message

//Reversing the Encoded

Str=Str2.toString();
int len= Str.length();
int a=0;
while(a<len)
Numbers

//Separating the Different

{
int b= (int)Str.charAt(a)-48;
arr[k]=(10*arr[k])+b;
in array

//Storing separte numbers

if((arr[k]>=65 && arr[k]<=90) || (arr[k]==32) || (arr[k]>=97 &&


arr[k]<=122))
k++;
a++;
}
}

void construct()

//To create decoded message

{
for(int a=0; a<k; a++)
{
Str3+=(char)arr[a];
}
}
void display()
{
System.out.println();
System.out.println("Decoded Message: ");
System.out.println();
System.out.println(Str3);
}
}
SAMPLE DATA:
AGENCY OF INTERNATIONAL EPISONAGE
Please Enter code:
2312179862310199501872379231018117927
Decoded Message:
Have a Nice Day

//To display Decoded Message

Potrebbero piacerti anche