Search This Blog

Sunday 27 April 2014

Java program to encode a given string.

import java.lang.String;
import java.util.*;
import java.io.*;

class Encode4
{

    public static void main(String args[]) throws IOException
    {
   
        int len,c,i,j,n;
        String str,keystr;

        DataInputStream in= new DataInputStream(System.in);
       
        int[] arr=new int[50];
        System.out.println("Enter the string to encode:");
        str=in.readLine();
        int key;
        System.out.println("Enter the Key:");
        keystr=in.readLine();
        key=Integer.parseInt(keystr);
         len= str.length();
        //System.out.println(key);

        for(i=0;i<len;i++)
        {
            c=str.charAt(i);
            if(c<91)
            {
                n=(c+key)%90;
                if(n<65)
                    n=n+64;
                arr[i]=n;
            }
            else
            {
                n=(c+key)%122;
                if(n<97)
                    n=n+96;
                arr[i]=n;
            }
        }
        System.out.println("The Encoded string is:");
       
        for(j=0;j<len;j++)
        {
            System.out.format("%c",arr[j]);
        }
    }

   
}

No comments :

Post a Comment

FREE Hit Counters