Write a short program,Java, that implements the Caesar substitution cipher with k=3. That is, each letter in the input plaintext should be shifted by 3 positions (e.g., a is replaced by d, b is replaced by e, and so on). Please show code indentation, comments, and output.
import java.util.Scanner; //CaesarShift3.java public class CaesarShift3 { public static String encrypy(String code, int i) { String result = ""; for(char ch: code.toCharArray()){ ch = (char) (ch + i); if(ch > 'z'){ ch = (char) ('a' + (ch%'z') - 1); } result += ch; } return result; } public static void main(String args[]){ Scanner scan = new Scanner(System.in); String s; System.out.print("Enter text: "); s = scan.nextLine(); System.out.println(encrypy(s,3)); } }
Get Answers For Free
Most questions answered within 1 hours.