I write a static public method called encrypt, that takes one string as an argum
ID: 3798178 • Letter: I
Question
I write a static public method called encrypt, that takes one string as an argument and returns a String. The method encrypts a word, by replacing each letter with the character two places ahead of it in the alphabet. The last two letters of the alphabet wrap around to the front (i.e. 'y 'a', 'z' 'b'). Assume that all input consists solely of lowercase letters, and that only one word is input. Sample output: System.out.println (encrypt ("ahadefxy.z" edefghAab System. out.println (encrypt ("sampletext.") Write a static public method called reverse, that accepts one String as an argument, returns nothing, and prints the reverse of the input. Sample output: reverse ("The quick brown fox jumps over the lazy dog.") god yaal eht Eevo spmuj *of nworb KGiug ehTExplanation / Answer
// Encryption.java
import java.util.Random;
import java.util.Scanner;
class Encryption
{
public static void encrypt(String s)
{
// string to char array
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length ;i++ )
{
if(s.charAt(i) == 'y')
charArray[i] = 'a';
else if(s.charAt(i) == 'z')
charArray[i] = 'b';
else
charArray[i] = (char)(s.charAt(i) + 2);
}
// array back to string
s = String.valueOf(charArray);
System.out.println(s);
}
public static void reverse(String s)
{
String rev = "";
int length = s.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
rev = rev + s.charAt(i);
System.out.println("Reversed string: "+rev);
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string to encrypt: ");
String str = scan.next();
encrypt(str);
scan.nextLine();
System.out.print("Enter a string to reverse: ");
String string = scan.nextLine();
reverse(string);
}
}
/*
output:
Enter a string to encrypt: sampletext
ucorngvgzv
Enter a string to reverse: The quick brown fox jumps over the lazy dog.
Reversed string: .god yzal eht revo spmuj xof nworb kciuq ehT
*/