본문 바로가기

프로그래머스/lv0

문자 반복 출력하기

/* 문자 반복 출력하기
 * 문자열 my_string과 정수 n이 매개변수로 주어질 때,
 * my_string에 들어있는 각 문자를 n만큼 반복한 문자열을 return
 *
 * my_string    n   result
 * "hello"      3   "hhheeellllllooo"
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class programmer_0_61 {
    static String a1 = "hello"static int b1 = 3;
    public String solution(String my_string, int n) {
        String answer = "";
        for(int i = 0; i < my_string.length(); i++){
             for(int k = 0; k < n; k++) answer += my_string.substring(i, i+1);
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_0_61 t = new programmer_0_61();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1,b1));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

문자열 뒤집기  (0) 2022.12.05
직각삼각형 출력하기  (0) 2022.12.05
배열 원소의 길이  (0) 2022.12.05
점의 위치 구하기  (0) 2022.12.05
최댓값 만들기 (1)  (0) 2022.12.05