Explanation :
For String =cde the Encrypted Code Shall be ASCII Values of c+9, d+9, e+9.
If n=abc then Output will be "jkl"
If n=az then Output will be "ji".
Code:
import java.util.Scanner;public class Encryption
{
String encryptString(String st)
{
char ch[]=st.toCharArray();
for(int i=0;i<st.length();i++)
{
if(ch[i]>='a' && ch[i]<='q')
ch[i]=(char) (ch[i]+9);
else
ch[i]=(char) (ch[i]-17); //(26-9)
}
String str=new String(ch);
return str;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1,str2;
System.out.println("Enter String");
str1=sc.next();
Encryption ecr=new Encryption();
str2=ecr.encryptString(str1);
System.out.println("Encrypted string is :"+str2);
sc.close();
}
}
No comments:
Post a Comment