public class Caesar01
{

public static void main(String[] args) {
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
SX GKB, OFOXDC YP SWZYBDKXMO KBO DRO BOCEVD YP DBSFSKV MKECOC

Why output are not--->
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
Please tell me.

Recommended Answers

All 5 Replies

Let's take the first word: "NS" and the letter 'N'
The alphabet goes:
"N", "O", "P", "Q", "R", "S"

You program returns: "SX": 'S'

As you can see if you add 5 to 'N' at the sequence I gave you (N, O, P, Q, R, S) the 5th letter after 'N' is 'S'. So program correctly returns: 'S' (SX)

But if you have subtracted 5:
"I", "J", "K", "L", "M", "N"
from 'N' you would have received the letter 'I' (IN)

Thank you
but
output
IN =AR, E<ENTS OF IMPORTANCE ARE THE RESULT OF TRI<IAL CAUSES
Why output are not--->
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES

Can you post again the new code?

Should we post here or here? You only need one thread.

Duplicate thread; closed. Refer the above post for the original version.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.