[C] 알고리즘 119 - Sentence Smash

박진현·2021년 10월 5일
1

알고리즘 (Javascript / C)

목록 보기
119/125

Q.

Sentence Smash
Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence!

Example
['hello', 'world', 'this', 'is', 'great'] => 'hello world this is great'

A)

#include <string.h>

char *smash(const char **words, size_t count)
{
  char *sentance = malloc(1000);
  int index = 0;
  
  for (int x = 0; x < count; x++) {
    for (int y = 0; words[x][y] != 0; y++) {
    sentance[index] = words[x][y];
    index++;
    }
    if (x + 1 != count) {
      sentance[index] = ' ';
      index++;
    }
  }
  sentance[index] = 0;
  return (sentance);
}

c언어나 자바스크립트나 알고리즘풀때 개똥같은건 같나보다

profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글