반응형
1. Scanner를 이용한 방법
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
String[] msg = new String[T];
for(int i=0; i<T; i++){
msg[i] = sc.next();
}
for(int i=0; i<msg.length; i++){
System.out.print(msg[i].charAt(0));
System.out.print(msg[i].charAt(msg[i].length()-1) + "\n");
}
sc.close();
}
}
2. BufferedReader를 이용한 방법
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));
int T = Integer.parseInt(br.readLine());
String[] msg = new String[T];
for(int i=0; i<T; i++){
msg[i] = br.readLine();
}
for(int i=0; i<msg.length; i++){
System.out.print(msg[i].charAt(0));
System.out.print(msg[i].charAt(msg[i].length()-1) + "\n");
}
br.close();
}
}
charAt()을 사용하되, 마지막 문자는 msg[i]의 길이에서 -1을 빼면 된다.
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 11720 - 숫자의 합 (0) | 2024.02.25 |
---|---|
[백준] 11654 - 아스키 코드 (0) | 2024.02.24 |
[백준] 2743 - 단어 길이 재기 (0) | 2024.02.22 |
[백준] 27866 - 문자와 문자열 (0) | 2024.02.21 |
[백준] 1546 - 평균 (0) | 2024.02.20 |