https://www.acmicpc.net/problem/11721
문제
풀이
👉 String.toCharArray() 을 이용한 방법
toCharArray() 메소드는 문자열을 한 글자씩 잘라서 char 배열 형태로 리턴한다.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] arr = br.readLine().toCharArray();
br.close();
for (int i = 1; i <= arr.length; i++) {
System.out.print(arr[i - 1]);
if (i % 10 == 0)
System.out.println();
}
}
}
👉 String.substring() 을 이용한 방법
substring() 메소드는 문자열 자르는 데 사용한다. 다음과 같이 2가지 형태로 사용할 수 있다.
- public String substring (int startIndex)
: startindex 부터 끝까지의 문자열을 리턴한다. (index는 0부터 시작)
- public String substring (int startIndex, int endIndex)
: startindex 부터 endIndex 전까지의 문자열을 리턴한다.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
br.close();
int n = s.length() / 10;
int index = 0;
for (int i = 0; i < n; i++) {
System.out.println(s.substring(index, index+10));
index += 10;
}
n = s.length() % 10;
System.out.println(s.substring(s.length()-n));
}
}
실행 결과