Encryption is
the technique of coding message to maintain their secrecy. A string of size ‘n’
where n is greator than 1 and less than 10, stores single sentences (each
sentence ends with a full Stop ) in each row of the array.
Write a program to
accept the size of the array. Display an appropriate message if the size is not
satisfying the given condition. Define a string array of the input size and
fill it with sentence row-wise.
Change the
sentence of the odd rows with an encryption of tho characters ahead of the original characters. Also change the
sentence of the even rows by sorting the sentence in reverse order/
Display the
encrypt sentences as per the sample data given below:
Test your
program with some sample data and some random data.
Input: N=4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS
FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCV JGT KU
HKPG.
COOL IS IT.
Solution:-
import java.util.*;
class sentence
{
String str[],s;
int i,n;
String input()
{
Scanner sc=new Scanner (System.in);
return
sc.nextLine();
}
void takeString()
{
Scanner
sc=new Scanner (System.in);
System.out.println("How
many sentences:");
n=sc.nextInt();
if(n<1||n>9)
{
System.out.println("INVALID
ENTRY:");
return;
}
str=new
String[n];
for(i=0;i<n;i++)
{
System.out.println("Enter
sentence (ends with full stop):");
s=input();
if(s.charAt(s.length()-1)!='.')
{
System.out.println("Full
stop at the end is mandatory:");
i--;
continue;
}
str[i]=s;
}
for(i=0;i<
n;i++)
{
s=str[i];
if(i%2==0)
encrypt(s);
else
reverse(s);
}
}
void
encrypt(String s)
{
int
i,len;
char
ch;
String
st="";
len=s.length();
for(i=0;i<
len;i++)
{
ch=s.charAt(i);
if((ch>=65
&& ch<=90)||(ch>=97 && ch<=122))
{
ch=(char)(ch+2);
if(ch
>90 && ch< 97)
{
ch=(char)((64+ch-90));
}
else
if(ch >122)
{
ch=(char)((96+ch-122));
}
}
st=st+ch;
}
System.out.println(st);
}
void reverse(String s)
{
int
i;
String
s1;
s=s.trim();
s=s.substring(0,s.length()-1);
while(true)
{
i=s.lastIndexOf("
");
if(i<0)
break;
s1=s.substring(i).trim();
System.out.print(s1+
" ");
s=s.substring(0,i).trim();
}
System.out.println(s+".");
}
public
static void main(String args[])
{
sentence
ob=new sentence();
ob.takeString();
}
}
No comments:
Post a Comment