Skip to content

Commit 2d5a9b1

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#412 from khalil2535/master
Update Caesar.java
2 parents f808a2b + ca8f6db commit 2d5a9b1

File tree

1 file changed

+127
-113
lines changed

1 file changed

+127
-113
lines changed

ciphers/Caesar.java

+127-113
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,131 @@
1-
/**
2-
Author : FAHRI YARDIMCI
1+
package ciphers;
32

4-
A Java implementation of Caesar Cipher.
5-
/It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. /
6-
**/
73
import java.util.Scanner;
4+
5+
/**
6+
*
7+
* A Java implementation of Caesar Cipher. /It is a type of substitution cipher
8+
* in which each letter in the plaintext is replaced by a letter some fixed
9+
* number of positions down the alphabet. /
10+
*
11+
* @author FAHRI YARDIMCI
12+
* @author khalil2535
13+
*/
814
public class Caesar {
9-
public static String encode (String message,int shift)
10-
{
11-
String encoded = "";
12-
for(int i = 0 ; i<message.length() ;i++)
13-
{
14-
int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet
15-
if(current==32)
16-
{
17-
encoded += " ";
18-
continue;
19-
20-
}
21-
else if (current>=65 && current<= 90)
22-
{
23-
int numAlphabet = message.charAt(i);
24-
if(shift + numAlphabet > 90)
25-
{
26-
int j = 90 - numAlphabet;
27-
char nextKey = (char)(65 + (shift - j - 1));
28-
encoded += nextKey;
29-
30-
}
31-
else
32-
{
33-
char nextKey = (char)(current + shift);
34-
encoded += nextKey;
35-
}
36-
}
37-
else if (current>=97 && current <= 122)
38-
{
39-
int numAlphabet = message.charAt(i);
40-
if(shift + numAlphabet > 122)
41-
{
42-
int j = 122 - numAlphabet;
43-
char nextKey = (char)(97 + (shift - j - 1));
44-
encoded += nextKey;
45-
}
46-
else
47-
{
48-
char nextKey = (char)(current + shift);
49-
encoded += nextKey;
50-
}
51-
}
52-
}
53-
return encoded;
54-
}
55-
public static String decode (String message,int shift)
56-
{
57-
String decoded = "";
58-
for(int i = 0 ; i<message.length() ;i++)
59-
{
60-
int current = message.charAt(i);
61-
if(current==32)
62-
{
63-
decoded += " ";
64-
continue;
65-
66-
}
67-
else if (current>=65 && current<= 90)
68-
{
69-
int numAlphabet = message.charAt(i);
70-
if(numAlphabet - shift < 65)
71-
{
72-
int j = numAlphabet - 65;
73-
char nextKey = (char)(90 - (shift - j - 1));
74-
decoded += nextKey;
75-
76-
}
77-
else
78-
{
79-
char nextKey = (char)(current - shift);
80-
decoded += nextKey;
81-
}
82-
}
83-
else if (current>=97 && current <= 122)
84-
{
85-
int numAlphabet = message.charAt(i);
86-
if(numAlphabet - shift < 97)
87-
{
88-
int j = numAlphabet - 97;
89-
char nextKey = (char)(122 - (shift - j - 1));
90-
decoded += nextKey;
91-
}
92-
else
93-
{
94-
char nextKey = (char)(current - shift);
95-
decoded += nextKey;
96-
}
97-
}
98-
}
99-
return decoded;
100-
}
101-
public static void main(String[] args)
102-
{
103-
Scanner input = new Scanner(System.in);
104-
System.out.println("Please enter the message (Latin Alphabet)");
105-
String message = input.nextLine();
106-
System.out.println(message);
107-
System.out.println("Please enter the shift number");
108-
int shift = input.nextInt() % 26;
109-
System.out.println("(E)ncode or (D)ecode ?");
110-
char choice = input.next().charAt(0);
111-
if(choice == 'E' || choice=='e')
112-
System.out.println("ENCODED MESSAGE IS \n" + encode(message,shift)); //send our function to handle
113-
if(choice =='D' || choice =='d')
114-
System.out.println("DECODED MESSAGE IS \n" + decode(message,shift));
115-
}
11615

117-
}
16+
/**
17+
* Encrypt text by shifting every Latin char by add number shift for ASCII
18+
* Example : A + 1 -> B
19+
*
20+
* @param message
21+
* @param shift
22+
* @return Encrypted message
23+
*/
24+
public static String encode(String message, int shift) {
25+
String encoded = "";
26+
27+
while (shift >= 26) { // 26 = number of latin letters
28+
shift -= 26;
29+
}
30+
31+
final int length = message.length();
32+
for (int i = 0; i < length; i++) {
33+
34+
// int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet
35+
char current = message.charAt(i); // Java law : char + int = char
36+
37+
if (IsCapitalLatinLetter(current)) {
38+
39+
current += shift;
40+
encoded += (char) (current > 'Z' ? current - 26 : current); // 26 = number of latin letters
41+
42+
} else if (IsSmallLatinLetter(current)) {
43+
44+
current += shift;
45+
encoded += (char) (current > 'z' ? current - 26 : current); // 26 = number of latin letters
46+
47+
} else {
48+
encoded += current;
49+
}
50+
}
51+
return encoded;
52+
}
53+
54+
/**
55+
* Decrypt message by shifting back every Latin char to previous the ASCII
56+
* Example : B - 1 -> A
57+
*
58+
* @param encryptedMessage
59+
* @param shift
60+
* @return message
61+
*/
62+
public static String decode(String encryptedMessage, int shift) {
63+
String decoded = "";
64+
65+
while (shift >= 26) { // 26 = number of latin letters
66+
shift -= 26;
67+
}
68+
69+
final int length = encryptedMessage.length();
70+
for (int i = 0; i < length; i++) {
71+
char current = encryptedMessage.charAt(i);
72+
if (IsCapitalLatinLetter(current)) {
73+
74+
current -= shift;
75+
decoded += (char) (current < 'A' ? current + 26 : current);// 26 = number of latin letters
76+
77+
} else if (IsSmallLatinLetter(current)) {
78+
79+
current -= shift;
80+
decoded += (char) (current < 'a' ? current + 26 : current);// 26 = number of latin letters
81+
82+
} else {
83+
decoded += current;
84+
}
85+
}
86+
return decoded;
87+
}
88+
89+
/**
90+
*
91+
* @param c
92+
* @return true if character is capital Latin letter or false for others
93+
*/
94+
private static boolean IsCapitalLatinLetter(char c) {
95+
return c >= 'A' && c <= 'Z';
96+
}
97+
98+
/**
99+
*
100+
* @param c
101+
* @return true if character is small Latin letter or false for others
102+
*/
103+
private static boolean IsSmallLatinLetter(char c) {
104+
return c >= 'a' && c <= 'z';
105+
}
106+
107+
/**
108+
*
109+
* @deprecated TODO remove main and make JUnit Testing
110+
*/
111+
public static void main(String[] args) {
112+
Scanner input = new Scanner(System.in);
113+
System.out.println("Please enter the message (Latin Alphabet)");
114+
String message = input.nextLine();
115+
System.out.println(message);
116+
System.out.println("Please enter the shift number");
117+
int shift = input.nextInt() % 26;
118+
System.out.println("(E)ncode or (D)ecode ?");
119+
char choice = input.next().charAt(0);
120+
switch (choice) {
121+
case 'E':
122+
case 'e':
123+
System.out.println("ENCODED MESSAGE IS \n" + encode(message, shift)); //send our function to handle
124+
break;
125+
case 'D':
126+
case 'd':
127+
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
128+
}
129+
}
130+
131+
}

0 commit comments

Comments
 (0)