본문 바로가기
알고리즘/백준

백준 11655번 - ROT13 (Java 8)

by latissimus 2022. 3. 14.

https://www.acmicpc.net/problem/11655

 

11655번: ROT13

첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.

www.acmicpc.net

코드 :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        StringBuilder sb = new StringBuilder();
        for(int i=0; i< line.length(); i++){
            char ch = line.charAt(i);
            if(ch == ' '){
                sb.append(" ");
                continue;
            }

            if (65 <= ch && ch <= 90) {
                char rot13 = (char) (ch + 13);
                if(ch > 77) {
                    rot13 %= 90;
                    rot13 += 64;
                }
                //77이하는 13 더해도 안넘어감.
                sb.append((char)rot13);
                continue;
            }
            if (97 <= ch && ch <= 122){
                char rot13 = (char) (ch + 13);
                if(ch > 109){
                    rot13 %= 122;
                    rot13 += 96;
                }

                sb.append((char)rot13);
                continue;
            }
            //숫자는 아무데도 안걸림
            sb.append(ch+"");
        }
        System.out.println(sb);
    }
}

 

if문 작성후 남는 부분을 숫자로 하려고 하다보니, 중복이 많아졌다.

 

댓글