재 현
시저암호 본문
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;
}
}
'Coding test' 카테고리의 다른 글
같은 숫자는 싫어 (0) | 2021.06.24 |
---|---|
제일 작은 수 제거하기 (0) | 2021.06.24 |
소수 찾기 (0) | 2021.06.22 |
이상한 문자 만들기 (0) | 2021.06.16 |
서울에서 김서방 찾기 (0) | 2021.06.15 |