[백준 10798 자바] 세로읽기

일단 해볼게·2023년 3월 11일
0

백준

목록 보기
101/132

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

import java.io.*;

public class Main {
    private static void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        char[][] arr = new char[5][15]; // 이차원 배열 생성

        for(int i = 0; i < arr.length; i++) {
            String input = br.readLine(); // 한 줄 읽기

            for (int j = 0; j < input.length(); j++) { // 읽은 문자열의 길이만큼 저장
                arr[i][j] = input.charAt(j); // 지정된 위치의 문자를 알려준다.
            }
        }

        for(int i = 0; i < 15; i++) {
            for (int j = 0; j < 5; j++) {
                if (arr[j][i] == '\0') // char의 초기 값 : \0 = null
                    continue;
                sb.append(arr[j][i]);
            }
        }
        System.out.println(sb);
    }
    public static void main(String[] args) throws Exception {
        solution();
    }
}
  • charAt은 지정된 문자 위치의 문자를 리턴한다.
  • char의 초기 값은 \0이어서 입력값 인덱스 이외의 값들은 \0으로 채워진다.
  • NULL이 아닐 때를 표현하고 싶다면 if(arr[j][i] != '\0')
profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글