C++)
#include <string>
#include <vector>
#include <cstring>
using namespace std;
string solution(string s, int n) {
string answer = "";
for(int i = 0; i < s.size(); i++){
int pos = s.at(i) + n;
if('a'<= s.at(i) && s.at(i)<='z'){
if(pos > 'z')
pos -= 26;
answer += pos;
}
else if('A'<= s.at(i) && s.at(i)<='Z'){
if(pos > 'Z')
pos -= 26;
answer += pos;
}
else
answer += " ";
}
return answer;
}
Python)
def solution(s, n):
result = ''
for ch in s:
pos = ord(ch)+n
if 'a' <= ch <= 'z':
if pos > ord('z'):
pos -= 26
result += chr(pos)
elif 'A' <= ch <= 'Z':
if pos > ord('Z'):
pos -= 26
result += chr(pos)
else:
result += ' '
return result'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 콜라 문제 (C++/Python) (0) | 2023.02.07 |
|---|---|
| [Level1] 푸드 파이트 대회 (C++/Python) (0) | 2023.02.07 |
| [Level 1] 수박수박수박수박수박수? (C++/Python) (0) | 2021.01.08 |
| [Level 1] 문자열을 정수로 바꾸기 (C++/Python) (0) | 2021.01.08 |
| [Level 1] 서울에서 김서방 찾기 (C++/Python) (0) | 2021.01.08 |