ericek111

slovicka

Sep 10th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5.  
  6. char* smash(const char** words, size_t count) {
  7.     size_t lengthStr = count;
  8.  
  9.     for (size_t i = 0; i < count; ++i) {
  10.         lengthStr += strlen(words[i]);
  11.     }
  12.  
  13.     char* newStr = malloc(lengthStr);
  14.     char* cursor = newStr;
  15.     for (int i = 0; i < count; ++i) {
  16.         ptrdiff_t len = strlen(words[i]);
  17.         memcpy(cursor, words[i], len);
  18.         cursor += len;
  19.         *cursor = ' ';
  20.         cursor++;
  21.     }
  22.     *cursor = '\0';
  23.  
  24.     return newStr;
  25. }
  26.  
  27. int main(int argc, char const *argv[]) {
  28.     const char *words[] = {
  29.         "hello", "world"
  30.     };
  31.  
  32.     char* smashed = smash(words, sizeof(words) / sizeof(words[0]));
  33.  
  34.     printf("%s\n", smashed);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment