package caesar;
import java.util.Scanner;
public class Caesar
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int key = scan.nextInt();
String str = scan.next();
String encrypted = encrypt(str, key);
System.out.println(encrypted);
}
CAUSES
public static String encrypt(String str, int key) {
String encrypted = " ";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c > 'Z')
c = c -26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c > 'z')
c = c -26;
}
encrypted += (char) c;
}
return encrypted;
}
}
Input
5
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
Output
IN
Why did not Output this.---->IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
But
package caesar;
import java.util.Scanner;
public class Caesar
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = "NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX";
int key = 5;
String encrypted = encrypt(str, key);
System.out.println(encrypted);
}
public static String encrypt(String str, int key) {
String encrypted = " ";
for(int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c > 'Z')
c = c -26;
} else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c > 'z')
c = c -26;
}
encrypted += (char) c;
}
return encrypted;
}
}
Output
IN =AR, E<ENTS OF IMPORTANCE ARE THE RESULT OF TRI<IAL CAUSES
Why did not Output this.---->IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
Please help me.