Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CaesarCipher {
  4. public static void main(String[] args){
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print("Your message? ");
  7. String input = sc.nextLine();
  8. System.out.print("Encoding Key? ");
  9. int key = sc.nextInt();
  10. sc.close();
  11.  
  12. System.out.print("Your message: ");
  13. encode(input, key);
  14. }
  15.  
  16. public static void encode(String input, int key){
  17. input = input.toUpperCase();
  18. for (int i = 0; i < input.length(); i++){
  19. char letter = input.charAt(i);
  20. if (letter == 32){
  21. System.out.print(" ");
  22. } else if (letter > 90){
  23. letter = (char) (letter - 26 + key);
  24. System.out.print(letter);
  25. } else {
  26. letter = (char) (letter + key);
  27. System.out.print(letter);
  28. }
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement