카타 8급 String repeat

1 C[ | ]

#include <string.h>
char *repeat_str(size_t count, const char *src) {
  char* res = calloc(strlen(src)*count, sizeof(char));
  for(int i=0; i<count; i++) strcat(res, src);  
  return res;
}
#include <string.h>
char *repeat_str(size_t count, const char *src) {
  char* res = calloc(strlen(src)*count, sizeof(char));
  while(count--) strcat(res, src);  
  return res;
}
#include <string.h>
char *repeat_str(size_t count, const char *src) {
  char* res = malloc(strlen(src)*count);
  res[0] = 0;
  for(int i=0; i<count; i++) strcat(res, src);  
  return res;
}
#include <string.h>
char *repeat_str(size_t count, const char *src) {
  int len = strlen(src);
  char* res = malloc(count * len * sizeof(char));
  for(int i=0; i<count; i++) strcpy(res+i*len, src);
  return res;
}

2 Kotlin[ | ]

fun repeatStr(r: Int, str: String): String {
  var s = ""
  for (i in 1..r) s += str
  return s
}
fun repeatStr(r: Int, str: String): String{
    var result: String = ""
    repeat(r){
        result += str
    }
    return result
}
fun repeatStr(r: Int, str: String) = str.repeat(r)
fun repeatStr(r: Int, str: String) = (1..r).map { str }.joinToString("")
fun repeatStr(r: Int, str: String) = r.downTo(1).joinToString("") { str }

3 PHP[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}