Write a program to input an encoded sentence having only sequence of ASCII values without any spaces. Decoded the encoded text and print in the form of a statement.

/**
 * The class decode decodes the encrypted text eneterd by the user
*/
 
SOURCE CODE :
 
import java.io.*;
class decode
{
static String str1;
static String str=new String();
static String dec=new String();
static int l;
 
decode()
{
l=0;
str="";
}
static void accept()throws IOException
{
InputStreamReader read= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(read);
l=0;
System.out.print("Enter the Encoded message : ");//2312179862310199501872379231018117927
str1=br.readLine();
for(int i=str1.length()-1;i>=0;i--)
str=str+str1.charAt(i);
System.out.println("Reversed message : "+str);//7297118101329732781059910132689712132
System.out.println("the length of the string is "+str.length());
}
 static void Decrypt()
    {
        
        int c=0;
        
        while(c<str.length())
        {
        if(str.charAt(c)!='1')
        {
            
            
            dec=dec+((char)(Integer.parseInt(str.substring(c,c+2))));
            c+=2;
        }
        else
        {
            
            dec=dec+((char)(Integer.parseInt(str.substring(c,c+3))));
            c+=3;
        }
        }
    }
        static void display()
        {
            System.out.print("The decrypted code is : "+dec);
        }
    public static void main()throws IOException
    {
        decode ob=new decode();
        ob.accept();
        ob.Decrypt();
        ob.display();
    }
}
        
Algorithm :
Start
Step1: Accept the encoded text from the user
Step2: Reverse the encoded code using for loop statement
Step3: Start a conditioned while-loop. Give the condition as c(counter)< string length .
Step4: Extract each character and check if it begins with one or not. If yes go to step 5 else go to step 6.
Step5: Extract the first three digit of the code using substring function and explicitly convert from string to character. Increase the counter by three.
Step6: Extract the first two digits of the code using substring function and explicitly convert from string to character. Increase the counter by two.
Stop
 

No comments:

Post a Comment