알고리즘 12 - Sentence Smash

jabae·2021년 10월 6일
0

알고리즘

목록 보기
11/97

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>
#include <stdlib.h>

char *smash(const char **words, size_t count) {
  
  int len = 0;
  size_t i = 0;
  
  while (i < count)
    len += strlen(words[i++]);
  
  char *result = malloc(sizeof(char) * (len + i));
  size_t j = 0;
  int y = 0;
  
  while (j < count)
  {
    int z = 0;
    while (words[j][z] != '\0')
      result[y++] = words[j][z++];
    if (j++ + 1 < count)
      result[y++] = ' ';
  }
  result[y] = '\0';
  return (result);
}
profile
it's me!:)

0개의 댓글