Ladies_Man

1_13 Concatenate strings

Dec 15th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. char *concat(char **s, int n)
  6. {
  7.     int i = 0, len = 0;
  8.     for(i = 0; i <= n; i++) len = len + strlen(s[i]);
  9.     len = len + 1;
  10.  
  11.     char *str = (char *)malloc(len);
  12.     strcpy(str, "");
  13.  
  14.     i = 0;
  15.     for(i = 0; i <= n; i++) strcat(str, s[i]);
  16.     return str;
  17. }
  18.  
  19. int main()
  20. {
  21.     char *str[100];
  22.     int i = 0, n;
  23.     scanf ("%d", &n);
  24.  
  25.     for(i = 0; i <= n; i++) str[i] = (char *)malloc(100);
  26.  
  27.     i = 0;
  28.     for(i = 0; i <= n; i++) gets(str[i]);
  29.  
  30.     char *temp_str = concat(str, n);
  31.     printf("%s", temp_str);
  32.  
  33.     for(i = 0; i <= n; i++) free(str[i]);
  34.     free(temp_str);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment