This is a rot-n (rotate n times) encrypt/decrypt algorithm, where n can be any integer (+/-).
It's written to encrypt/decrypt all the alphabets (upper/lower case) and leaving behind all non-alphas.
rot-n where n is the key, is given in the first line of input file.
Everything is working fine. I just need help in optimizing it.
Challenge description
Your submission will be tested against an input file that contains ASCII characters. The input file starts at the very first line with a N-digits positive or negative integer number that is your cipher, followed by a new-line character. The second line starts with the payload of the encrypted text until the end of file. Note that only words (alphanumeric sequences) are encrypted and that every other character (i.e. punctuation) must not be processed by your algorithm and must also be copied ‘as is’ to the standard output.*
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Fightsopa {
public void process(String input, int n) {
int abcd = n;
if (abcd < 0) {
abcd = (26 - ((0 - abcd) % 26));
}
int rot = 26 - abcd % 26; //for decrypting
// int rot= abcd % 26; //for encrypting
char out = 0;
for (char ch : input.toCharArray()) {
if (ch >= 65 && ch <= 90) {
if (ch + rot <= 90) {
out = (char) (ch + rot);
System.out.print(out);
} else {
out = (char) (ch + rot - 26);
System.out.print(out);
}
} else if (ch >= 97 && ch <= 122) {
if (ch + rot <= 122) {
out = (char) (ch + rot);
System.out.print(out);
} else {
out = (char) (ch + rot - 26);
System.out.print(out);
}
} else {
System.out.print(ch);
}
}
}
public void handler(File fFile) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(fFile));
int rot = 0;
int count = 0;
while (scanner.hasNextLine()) {
if (count == 0) {
rot = scanner.nextInt();
scanner.nextLine();
} else {
String wordp = scanner.nextLine();
process(wordp, rot);
System.out.println();
}
count++;
}
scanner.close();
}
public static void main(String args[]) throws FileNotFoundException {
if (args.length > 0) {
File fFile = new File(args[0]); //Input file having the first line as the key
Fightsopa fsopa = new Fightsopa();
fsopa.handler(fFile);
}
}
}