Hi friends,
the code for life we discuss here is,
the code for life we discuss here is,
SQUARE ENCRYPT
A
secret code encrypts a message by putting it in an array and reading down the
columns (blanks are replaced by asterisks and full stops are added to fill up
the array).
Write
a program that encrypts an input string. (For extra marks write a program that
decrypts a given string using the rules outlined above.)
LETS*G
O*TO*T HE*SAN DWICH* SHOP*T ODAY..
Input: “Lets
go to the sandwich shop today”
Output: “Lohdsoe*ewhdtt*ioasoscpy**ah*.gtn*t.”
CODE:
package sanjay;
import java.util.*;
public class encrypt {
public static
void main(String args[])
{
Scanner
sc=new Scanner(System.in);
System.out.println("Enter a string : ");
String
s1=sc.nextLine();
int
length=s1.length();
String
s2[]=new String[100]; //to store
as a char array
for(int
z=0;z<s2.length;z++)
{
s2[z]=""; //2nd array s 1st
position
}
int
count=0;
int j=0;
for(int
i=0;i<s1.length();i++)
{
if(s1.charAt(i)==' ')
//if space then
{
String str="*";
//assign *
s2[j]=s2[j]+str; //to
link wid next word
count++;
}
else
{
s2[j]=s2[j]+s1.charAt(i);
//1&2nd arrays 1st pos +
count++;
}
if(count!=6&&i==s1.length()-1)
{
for(int x=count;x<6;x++)
{
s2[j]=s2[j]+".";
//to fill blanks wid .
}
}
if(count==6)
{
j++;
count=0;
}
}
for(int
k=0;k<6;k++)
{
for(int y=0;y<=j;y++)
{
System.out.print(s2[y].charAt(k));
}
}
}
}