File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package hackerrank ;
2+
3+ public class CaesarCipher {
4+
5+ static String caesarCipher (String s , int k ) {
6+
7+ StringBuffer result = new StringBuffer ();
8+
9+ for (int i = 0 ; i < s .length (); i ++) {
10+ //대문자
11+ if (Character .isUpperCase (s .charAt (i ))) {
12+ char ch = (char ) (((int ) s .charAt (i ) + k - 65 ) % 26 + 65 );
13+ result .append (ch );
14+ //소문자
15+ } else if (Character .isLowerCase (s .charAt (i ))) {
16+ char ch = (char ) (((int ) s .charAt (i ) + k - 97 ) % 26 + 97 );
17+ result .append (ch );
18+ //기타 특수문자등
19+ } else {
20+ result .append (s .charAt (i ));
21+ }
22+ }
23+ return result .toString ();
24+ }
25+
26+ public static void main (String [] args ) {
27+ System .out .println (caesarCipher ("a-z" , 2 ) + ", ans: c-b" );
28+ //System.out.println(caesarCipher("middle-Outz", 2) + ", ans: okffng-Qwvb");
29+ //System.out.println(caesarCipher("Hello_World!", 4) + ", ans: Lipps_Asvph!");
30+ //System.out.println(caesarCipher("www.abc.xy", 87) + ", ans: fff.jkl.gh");
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments