본문 바로가기

프로그래머스 코딩(자바)/Coding dojang

시저 암호 풀기

728x90

시저 암호는, 고대 로마의 황제 줄리어스 시저가 만들어 낸 암호인데,

예를 들어 알파벳 A를 입력했을 때,

그 알파벳의 n개 뒤에 오는(여기서는 예를 들 때 3으로 지정하였다)

알파벳이 출력되는 것이다.

예를 들어 바꾸려는 단어가 'CAT"고, n을 5로 지정하였을 때 "HFY"가 되는 것이다.

어떠한 암호를 만들 문장과 n을 입력했을 때

암호를 만들어 출력하는 프로그램을 작성해라.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
public class Exam0405_2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("암호입력 : ");
        String password = sc.nextLine();
        System.out.print("숫자입력 : ");
        int n = sc.nextInt();
        String newPassword = "";
        for(char ch : password.toCharArray()) {
            newPassword += (char)(ch + n);
        }
        System.out.println("만들어진 암호 : " + newPassword);
        sc.close();
    }
}
 
cs

 

728x90