Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 시저 암호 본문

Java

프로그래머스: 시저 암호

Daeryuk Kim 2024. 3. 2. 17:11
import java.util.*;

class Solution {
    public String solution(String s, int n) {
        StringBuilder answer = new StringBuilder();
        for (int i=0; i<s.length(); i++) {
            char x = s.charAt(i);
            
            if (x == ' ') {
                answer.append(' '); 
                continue;
            }
            
            if (Character.isLowerCase(x)) { // 소문자인 경우
                x = (char) ((x - 'a' + n) % 26 + 'a');
            } else { // 대문자인 경우
                x = (char) ((x - 'A' + n) % 26 + 'A');
            }
            
            answer.append(x);
        }
        return answer.toString();
    }
    
    public static void main(String[] args) {
        String s = "a B z";
        int n = 4;
        Solution sol = new Solution();
        String result = sol.solution(s, n);
        
        System.out.print(result); // 출력: "e F d"
    }
}