Advertisement
Pluto2097

Untitled

Mar 30th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. //Rasim Mollayev (ID: 2584)
  2. //ADA University
  3. //BCE 2018
  4. //CIS 101-Introduction to CS in Java
  5.  
  6. import java.util.Scanner; // import Scanner in order to get an input
  7. public class CaesarCipher {
  8.  
  9. static String caesar(String value, int shift) { //Created method caesar with two values - string & int
  10. //value is a value of - String
  11. //shift is an adjusted integer
  12. char[] buffer = value.toCharArray(); //Here opens an array (set of elements) and a function toCharArray fills symbols of String for example: if String is "Caesar", than array will be {C, a, e, s, a, r}
  13.  
  14. for (int i = 0; i < buffer.length; i++) {
  15. char letter = buffer[i];
  16. if (letter < 'A' || letter > 'Z' && letter < 'a' || letter > 'z') {
  17. buffer [i] = letter;
  18. } else {
  19. letter = (char) (letter + shift);
  20. if (letter > 'Z' && letter < 'a' || letter > 'z') {
  21. letter = (char) (letter - 26);
  22. } else if (letter < 'A' || letter > 'Z' && letter < 'a' ) {
  23. letter = (char) (letter + 26);
  24. } //finishing else
  25. buffer[i] = letter;
  26. }
  27. }
  28.  
  29. return new String(buffer); //give out all buffers in conclusion we get String
  30. }
  31.  
  32. private static Scanner in;
  33. public static void main(String[] args) {
  34.  
  35. in = new Scanner(System.in);
  36.  
  37. System.out.print("Enter your cipher text here: "); //headline for an input
  38. System.out.println("\nUnmixed text: "+caesar(in.nextLine(), +2)); //headline for an output (result-decoded text)
  39.  
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement