"카타 8급 Sentence Smash"의 두 판 사이의 차이

잔글 (봇: 카타 8급 PHP 분류 추가)
잔글 (봇: 자동으로 텍스트 교체 (-분류:PHP +))
57번째 줄: 57번째 줄:
</source>
</source>


[[분류:PHP]]
 
[[분류:카타 8급 C]]
[[분류:카타 8급 C]]
[[분류:카타 8급 PHP]]
[[분류:카타 8급 PHP]]

2019년 2월 3일 (일) 04:03 판

1 개요

카타 8급 C
# 🔗 문제 풀이

틀:카타 8급-4

2 C

#include <string.h>
char *smash(const char **words, size_t count) {
  char *res = malloc(1000);
  res[0] = '\0';
  for(int i=0; i<count; i++) {
    strcat(res, words[i]);
    if(i<count-1) strcat(res, " ");
  }
  return res;
}
#include <string.h>
char *smash(const char **words, size_t count) {
  int len = count;
  for(int i=0; i<count; i++) len += strlen(words[i]);
  char* res;
  res = (char*) malloc(len);
  res[0] = '\0';
  for(int i=0; i<count; i++) {
    strcat(res, words[i]);
    if(i<count-1) strcat(res, " ");
  }
  return res;
}
#include <string.h>
char *smash(const char **words, size_t count) {
  char *res= malloc(1000);
  int idx = 0;
  for(int i=0; i<count; i++) {
    for(int pos=0; words[i][pos]!=0; pos++) {
      res[idx] = words[i][pos]; idx++;
    }
    if(i+1 != count) {
      res[idx] = ' '; idx++;
    }
  }
  res[idx] = 0;
  return res;
}

3 PHP

function smash(array $words): string {
  return implode(' ', $words);
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}