(문제풀이)

백준 9086 (Java) : 문자열

cogito-new 2023. 11. 29. 20:28
import java.util.Scanner;

public class Main_9086 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int N = scanner.nextInt();
        String text = "";
        for(int i = 0; i<N; i++){
            text = scanner.next();

            System.out.print(Character.toString(text.charAt(0)));
            System.out.print(Character.toString(text.charAt(text.length()-1)));
            System.out.println();
        };
    };
};

 

1. 입력 : 주어진 예제에서 알파벳이 아닌 숫자 존재 -> next 메소드 사용

2. 출력시 CharAt(index) 로 잘라 주었기 때문에 Character.toString 메소드로 String type 으로 출력

   마지막 글자의 경우는 문자열.length 로 계산한 값이므로 인덱스를 구하기 위해 -1 처리

앞, 마지막 글자 출력후 엔터가 필요하므로 엔터 처리

반응형