250x250
반응형
Recent Posts
Recent Comments
Link
«   2024/10   »
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
Archives
Today
Total
관리 메뉴

재 현

시저암호 본문

Coding test

시저암호

본명은이점례 2021. 6. 23. 15:05
728x90

class Solution {
    public String solution(String s, int n) {
        String answer = "";
        char[] arr = s.toCharArray();
        for (char c : arr)
        {
         int p = c+n-26;
     char convertedChar = (char)p;
    
     int q = c+n;
     char convertedChar2 = (char)q;
    
            // 1. A~Z라면
            if(c>='A' && c<='Z')
            {

             if(c+n>90) 
             {   
            
             answer += convertedChar;
             } else {
             answer += convertedChar2;
             }
            }
                
            // 2. a~z라면
            else if(c>='a' && c<='z')
            {
             if(c+n>122)
             {
             answer += convertedChar;
             } else {
             answer += convertedChar2;
             }
            }
            
            // 3. 공백이라면
            else
                answer += " ";
        }
        return answer;
    }
}

728x90

'Coding test' 카테고리의 다른 글

같은 숫자는 싫어  (0) 2021.06.24
제일 작은 수 제거하기  (0) 2021.06.24
소수 찾기  (0) 2021.06.22
이상한 문자 만들기  (0) 2021.06.16
서울에서 김서방 찾기  (0) 2021.06.15