1+ /**
2+ Author : FAHRI YARDIMCI
3+
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+ **/
7+ import java .util .Scanner ;
8+ 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+ }
116+
117+ }
0 commit comments