Caesar Cipher

Caesar Cipher

 public static String caesarCipher(String input, int rotationFactor) {
    // Write your code here
StringBuilder sb = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') {
                char newChar = (char) (input.charAt(i) + rotationFactor%26);
                sb.append((newChar >= 'a' && newChar <= 'z') ? newChar : (char) (newChar - 'z' - 1 + 'a'));
            } else if (input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') {
                char newChar = (char) (input.charAt(i) + rotationFactor%26);
                sb.append((newChar >= 'A' && newChar <= 'Z') ? newChar : (char) (newChar - 'Z' - 1 + 'A'));
            } else
                sb.append(input.charAt(i));
        }
        return sb.toString();
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s