Advertisement
STANAANDREY

concat

Feb 28th, 2023
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5.  
  6. char *concat(int n, ...) {
  7.   va_list al;
  8.   va_start(al, n);
  9.   int len = 1;
  10.   char *res = calloc(sizeof(char), n);
  11.   for (int i = 0; i < n; i++) {
  12.     char *s = va_arg(al, char*);
  13.     len += strlen(s) + 1;
  14.     res = realloc(res, sizeof(char) * len);
  15.     strcat(res, s);
  16.     strcat(res, " ");
  17.   }
  18.   va_end(al);
  19.   return res;
  20. }
  21.  
  22.  
  23. int main(void) {
  24.   char *res = concat(3, "asd", "qwerty", "6543210");
  25.   puts(res);
  26.   free(res);
  27.   return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement