You are on page 1of 2

import java.util.

*;

class TransCipher {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the plain text: ");
String pl=sc.nextLine();

int start=0;
String s="";

for(int i=0;i<pl.length();i++) {
if(pl.charAt(i)==' ') {
s=s+pl.substring(start,i);
start=i+1;
}
}
s=s+pl.substring(start);

System.out.println("enter \"1\" for encryption and \"2\" for decryption:


");
int choice=sc.nextInt();
if (choice==1){
encryption(s);
}
else if (choice==2) {
decryption(pl);
}
else
System.out.println("please type the only \"1\" and \"2\": ");
}
public static void encryption(String s){
String even="";
String odd="";

for(int i=0;i<s.length();i++) {
if(i%2==0) {
even=even+s.charAt(i);
}
else {
odd=odd+s.charAt(i);
}
}
System.out.println(even.concat(odd));
}
public static void decryption(String s){
String pl="";
int lg=s.length();

if(lg%2==0) {
for (int i = 0; i < lg / 2; i++)
pl = pl + s.charAt(i) + s.charAt(i + lg / 2);
}
else {
for (int i=0;i<=lg/2;i++){
if (lg/2+i+1!=s.length()){
pl=pl+s.charAt(i)+s.charAt(i+lg/2+1);
}
else
pl=pl+s.charAt(i);
}
}
System.out.println(pl);
}
}

You might also like