/* * The java program that prompts user to * enter a string and shift key and then * converts the string to the encoded string * and prints the encoded string value to console. * */ //CeaserCipher.java import java.util.Scanner; public class CeaserCipher { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String message; int shift; System.out.println(\"Enter a message in small letters,a-z\"); message=scanner.nextLine(); System.out.println(\"Enter shift value\"); shift=scanner.nextInt(); //calling encode method with message, shift value String encodedMsg=encode(message,shift); //calling WriteMessage WriteMessage(encodedMsg); } /*Method that prints the encoded text to console*/ private static void WriteMessage(String encodedMsg) { System.out.println(\"Encoded text :\"+encodedMsg); } //Method that takes string and integer value and returns //the encoded string to calling method private static String encode(String message, int shift) { StringBuffer sb=new StringBuffer(message); for (int i = 0; i < message.length(); i++) { //shift to back to the alphabets char encodedkey = (char) (\'a\' + (message.charAt(i) -\'a\' + shift) % (\'z\' - \'a\' + 1)); //set character at index,i sb.setCharAt(i, encodedkey); } //return encoded string to calling method return new String(sb); } } Sample Output: Enter a message in small letters,a-z sunday is holiday Enter shift value 5 Encoded text :xzsifdYnxYmtqnifd Solution /* * The java program that prompts user to * enter a string and shift key and then * converts the string to the encoded string * and prints the encoded string value to console. * */ //CeaserCipher.java import java.util.Scanner; public class CeaserCipher { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String message; int shift; System.out.println(\"Enter a message in small letters,a-z\"); message=scanner.nextLine(); System.out.println(\"Enter shift value\"); shift=scanner.nextInt(); //calling encode method with message, shift value String encodedMsg=encode(message,shift); //calling WriteMessage WriteMessage(encodedMsg); } /*Method that prints the encoded text to console*/ private static void WriteMessage(String encodedMsg) { System.out.println(\"Encoded text :\"+encodedMsg); } //Method that takes string and integer value and returns //the encoded string to calling method private static String encode(String message, int shift) { StringBuffer sb=new StringBuffer(message); for (int i = 0; i < message.length(); i++) { //shift to back to the alphabets char encodedkey = (char) (\'a\' + (message.charAt(i) -\'a\' + shift) % (\'z\' - \'a\' + 1)); //set character at index,i sb.setCharAt(i, encodedkey); } //return encoded string to calling method return new String(sb); } } Sample Output: Enter a message in small letters,a-z sunday is holiday Enter shift value 5 Encoded text :xzsifdYnxYmtqnifd.