문제 설명
문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string에 들어있는 각 문자를 n만큼 반복한 문자열을 return 하도록 solution 함수를 완성해보세요.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* solution(const char* my_string, int n) {
size_t len = strlen(my_string);
char* answer = (char*)malloc(len * n + 1);
int idx = 0;
for (size_t i = 0; i < len; i++) {
for (int j = 0; j < n; j++) {
answer[idx++] = my_string[i];
}
}
answer[idx] = '\0';
return answer;
}'Coding Test' 카테고리의 다른 글
| signal: floating point exception (core dumped) (0) | 2024.07.10 |
|---|---|
| 순서쌍의 개수 (0) | 2024.07.09 |
| 세균 증식 (0) | 2024.07.07 |
| 삼각형의 완성조건 (1) (0) | 2024.07.06 |
| 모음 제거 (0) | 2024.07.05 |