Sunday, January 23, 2011

BlueJ Program On Decoding Encoded String


BlueJ Program on decoding encoded String with removing extra blank spaces and modification on alphabet cases.


The program is as stated below

An encoded string can be decoded by finding actual character for the given ASCII code in the encoded message. Write a program to input an encoded text having only
sequence of ACSII values without any spaces.Any code or value which is not in the range(65-90 or 97-122 or 32 for space) will be ignored and should not appear in the output message.Decode the encoded text and print in the form of sentence.The first alphabet of each word must be in capitals and rest alphabets will be in lower case
only. Any consecutive sets of code 32 will be taken as only one blank space.
The output should be exactly in the following format:

Input (encoded string) : 10665771011153266797868
Output(decoded string) : Space removed=jAMes BOND
Coded value=James@bond

Input (coded string) : 78487367421013232321006589
Output(decoded string) : Nice Day

This string program involves three steps.
  1. Decoding the encoded text with only the alphabets and blank spaces, eliminating the others.
  2. Elimination of extra blank spaces from the decoded text.
3. Changing the cases of the alphabets in the decoded text.

How to proceed on decoding the encoded text

Take the string of ASCII values in a string object. Firstly retrieve three characters from the left side of encoded text and check if is within the specified range, means it is the ASCII value of any alphabet or that of blank space. If the checking succeeds, concatenate the equivalent alphabet with another string object which will represent the decoded string and at the same time eliminate that ASCII substring from the original ASCII string. If the checking shows that the retrieved substring is not the ASCII value of the specified range then try the above process with two left most characters from the ASCII string and follow the same actions which are to be performed for three character substring. If the ASCII substring matches with any alphabet or blank space, remove the two left most characters from the original string.

If both the checkings with three left most characters and two left most characters shows that the substring is not the ASCII value of any alphabet or blank space remove two left most characters from the original string and continue the process until the length of the original string becomes less than 2.

At the end the second string object will represent the decoded string value with extra blank spaces in it if it were in the ASCII string and the case of the characters in the decoded string will be as per ASCII values in the entered encoded ASCII string.  

 How to proceed to remove extra blank spaces from the decoded string

Each character of the decoded string is to be accessed and appended to another string object excepting consecutive spaces. This process will eliminate all the extra blank spaces if any from the decoded text.

 How to proceed to change the case of alphabets in decoded string

Here also loop is required. Access every alphabet from the decoded string using loop. Check the case of first character of the decoded string. If it is in lower case, chande it in to upper case and whenever any blank space is found in the decoded string, perform the operation as the first alphabet of the decoded string. For the rest alphabets of the decoded string, checking is to be done and for upper case apphabets they should be converted to lower case alphabet.

 Codes of the encoded and decoded string program

import java.io.*;
class Words2
{
 int w;
 BufferedReader br;
 String text,text1="";
  public static void main(String args[])throws IOException
   {
  Words2 ob=new Words2();
 ob.accept();
 ob.result();
  }
  Words2()
  {
   br=new BufferedReader(new InputStreamReader(System.in));
   text="";
   w=0;
  }
public void accept()throws IOException
{
 System.out.println("Enter the Coded value:");
 text=br.readLine().trim();
 }
public void result()
{
int i,len;
String ch;
 System.out.println("The encoded text ="+text);
  do
 {
 if(text.length()< 3)
  ch=text.substring(0,2);
  else
   ch=text.substring(0,3);
   System.out.println(ch);
   if( Integer.parseInt(ch)< =122 && Integer.parseInt(ch)>=97)
   {
   text1=text1+(char)(Integer.parseInt(ch));
   text=text.substring(3);
   }
   else
   {
   ch=text.substring(0,2);
   if( Integer.parseInt(ch)< =90 && Integer.parseInt(ch) >=65)
   {
   text1=text1+(char)(Integer.parseInt(ch));
   text=text.substring(2);
   }
 else if( Integer.parseInt(ch)==32)
   {
   text1=text1+" ";
   text=text.substring(2);
   }
   else
  text=text.substring(2);
   }
   }while(text.length() >=2);
text1=removeSpace(text1);
System.out.println("Space removed="+text1);
text1=changeCase(text1);
System.out.println("Coded value="+text1);
}
String removeSpace(String s)
{
 int j,len,flag=0;
 String str="";
 s=s.trim();
 len=s.length();
 for(j=0;j< len;j++)
 {
  if(s.charAt(j)==' ' && flag==1)
  {
  str=str+s.charAt(j);
  flag=0;
  }
  else if(s.charAt(j)!=' ')
  {
   flag=1;
   str=str+s.charAt(j);
  }
  }
  return str;
 }
String changeCase(String s)
{
 int j,len,flag=0;
 String str="";
 s=s.trim();
 len=s.length();
 for(j=0;j< len;j++)
 {
  if(s.charAt(j) >=97 && j==0)
  str=str+(char)(s.charAt(j)-32);
  else if(s.charAt(j)==48)
  {
  str=str+" ";
  flag=1;
  }
  else if(s.charAt(j)< =90 && flag==0)
  str=str+(char)(s.charAt(j)+32);
  else if(s.charAt(j) >=97 && flag==1)
  {
  str=str+(char)(s.charAt(j)-32);
  flag=0;
  }
  else
  {
   str=str+s.charAt(j);
  }
  }
  return str;
 }
 }

7 comments:

  1. mine is shorter... n i'm a class XI student

    import java.io.*;
    import java.util.*;
    class coding1
    {
    public void method (String args[])throws IOException
    {
    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    String s=""; int ch1=32; char c; int i;
    System.out.print("Input (coded text)\t:");
    String cod=buf.readLine();
    int length=cod.length();
    int ch=0,a1=2,flag=0;
    for (int a=0;a<=(length-1);)
    {
    ch=(Integer.parseInt(cod.substring(a,a1)));

    if(((ch>=65)&&(ch<=90))||(ch==32)||((ch>=97)&&(ch<=122)))
    {

    if (ch1==32)
    {
    ch=((ch>=65)&&(ch<=90))? ch:(ch-32);
    }
    else
    ch=((ch>=65)&&(ch<=90))? (ch+32):ch;
    c=(char)ch;
    s=s+c;

    ch1=ch;
    a+=(flag==0)?2:3;
    a1+=2;
    flag=0;

    }
    else
    {if(flag==0)
    {a1++;
    flag=1;
    continue;}
    else
    {flag=0; a+=2; a1++; continue;}

    }

    }
    System.out.println("Output(decoded text)\t:");
    StringTokenizer st=new StringTokenizer(s);
    for (i=0;i<=(st.countTokens()+1);i++)
    System.out.print ((st.nextToken()).trim()+" ");
    }

    }

    ReplyDelete
    Replies
    1. Man i did this in class 9 th project...

      Delete
  2. Mine is shortest....excluding the main......
    public class Encryption
    {
    public String encryptDecrypt(String s,int move)
    {
    String str=new String();
    for(int i=0;i<s.length();i++)
    {
    char c=s.charAt(i);
    if(Character.isLetter(c))
    {
    int n;
    if(Character.isUpperCase(c))
    {
    n=c-'A';
    }
    else
    {
    n=c-'a';
    }
    n=(n+move)%26;
    if(n<0)
    n+=26;
    if(Character.isUpperCase(c))
    {
    c=(char)(n+'A');
    }
    else
    {
    c=(char)(n+'a');
    }
    }
    str+=c;
    }
    return str;
    }
    }

    ReplyDelete
    Replies
    1. what does "move" do?

      Delete
    2. This program is simple:

      class de_code
      {
      public void de_coder(String code)
      {
      int c=0;
      while(c<code.length())
      {
      char s=code.charAt(c);
      if (s!='1')
      {
      String S1= code.substring(c,c+2);
      int p=Integer.parseInt(S1);
      char p1= (char)p;
      System.out.print(p1);
      c+=2;
      }
      else
      {
      String S1= code.substring(c,c+3);
      int p=Integer.parseInt(S1);
      char p1= (char)p;
      System.out.print(p1);
      c+=3;
      }
      }
      }
      }

      Delete
  3. its output is jAMes BOND

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner