Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rasim Mollayev (ID: 2584)
- //ADA University
- //BCE 2018
- //CIS 101-Introduction to CS in Java
- import java.util.Scanner; // import Scanner in order to get an input
- public class CaesarCipher {
- static String caesar(String value, int shift) { //Created method caesar with two values - string & int
- //value is a value of - String
- //shift is an adjusted integer
- 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}
- for (int i = 0; i < buffer.length; i++) {
- char letter = buffer[i];
- if (letter < 'A' || letter > 'Z' && letter < 'a' || letter > 'z') {
- buffer [i] = letter;
- } else {
- letter = (char) (letter + shift);
- if (letter > 'Z' && letter < 'a' || letter > 'z') {
- letter = (char) (letter - 26);
- } else if (letter < 'A' || letter > 'Z' && letter < 'a' ) {
- letter = (char) (letter + 26);
- } //finishing else
- buffer[i] = letter;
- }
- }
- return new String(buffer); //give out all buffers in conclusion we get String
- }
- private static Scanner in;
- public static void main(String[] args) {
- in = new Scanner(System.in);
- System.out.print("Enter your cipher text here: "); //headline for an input
- System.out.println("\nUnmixed text: "+caesar(in.nextLine(), +2)); //headline for an output (result-decoded text)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement