본문 바로가기
알고리즘 & 자료구조/프로그래머스

시저암호

by 신재권 2022. 10. 20.
package programmers;

public class 시저암호 {

   public static String solution(String s, int n) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < s.length(); i++) {
         char c = s.charAt(i);
         if (c >= 'a' && c <= 'z') {
            c += n;
            if (c > 'z') {
               int a = c - 'z';
               c = (char)('a' - 1 + a);
            }
            sb.append(c);
         } else if (c >= 'A' && c <= 'Z') {
            c += n;
            if (c > 'Z') {
               int a = c - 'Z';
               c = (char)('A' - 1 + a);
            }
            sb.append(c);
         } else {
            sb.append(' ');
         }
      }
      return sb.toString();
   }

   public static void main(String[] args) {
      System.out.println(solution("AB", 1).equals("BC"));
      System.out.println(solution("z", 1).equals("a"));
      System.out.println(solution("a B z", 4).equals("e F d"));
   }
}


'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

최소직사각형  (0) 2022.10.23
비밀지도  (0) 2022.10.22
예산  (0) 2022.10.20
124 나라의 숫자  (0) 2022.06.24
멀쩡한 사각형  (0) 2022.06.23