https://programmers.co.kr/learn/courses/30/lessons/12926

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s, int n) {
    bool isUpper;
    for (int i = 0; i < s.length(); i++)
    {
        //해당 문자가 공백이면 다음 문자로 넘어간다.
        if (s[i] == ' ')continue;
 
        //해당 문자의 대소문자를 판단한다.
        if (s[i] >= 'A' && s[i] <= 'Z')
        {
            isUpper = true;
        }
        if (s[i] >= 'a' && s[i] <= 'z')
        {
            isUpper = false;
        }
 
        //n만큼 알파벳을 밀어낸다.
        s[i] += n;
 
        if (isUpper == true)
        {
            //대문자일 경우 Z보다 큰지 비교하고, 클 경우 A와 Z의 차(26)만큼 빼준다.
            if ((unsigned int)s[i] > 'Z')
            {
                s[i] -= 26;
            }
        }
        if (isUpper == false)
        {
            //소문자일 경우 z보다 큰지 비교하고, 클 경우 a와 z의 차(26)만큼 빼준다.
            if ((unsigned int)s[i] > 'z')
            {
                s[i] -= 26;
            }
        }
    }
    return s;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

해당 알파벳의 n만큼 밀어내는 것은 간단하지만,

해당 알파벳의 대소문자를 판별하고,

밀어냈을때의 아스키 코드 값이 a~z 혹은 A~Z 범위를 넘어가는지 검사하고 예외처리를 해야 한다.

 

획득 점수 = 6점

+ Recent posts