프로그래머스 레벨 1 테스트

 

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
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s, int n) {
    string answer = "";
    
    for(int i = 0 ; i < s.size() ; i++)
    {
        //공백 처리
        if(s[i] == ' ')
        {
            answer += " ";
            continue;
        }
        
        //아스키코드 변환
        int ascii = s[i];
        
        for(int j = 0 ; j < n ; j++)
        {
            ascii++;
            if(ascii == 'z' +1 || ascii == 'Z' +1)
                ascii -= 26;
        }
        
        answer += ascii;
    }
    
    return answer;
}
 

문자를 ascii 코드로 변환해 코드의 값을 비교해서 문제 해결

+ Recent posts