Guest User

Untitled

a guest
Dec 4th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 50
  5.  
  6. void arrayOfChars(){
  7.  
  8.     printf("Введите размер массива\n");
  9.     int size = 0;
  10.     char buf2[1024];
  11.     fgets(buf2, 1024, stdin);
  12.     size = atoi(buf2);
  13.     // scanf("%d", &size);
  14.     char *p = (char *) malloc(size * sizeof(char));
  15.     printf("Динамический массив создан\n");
  16.     for (int i = 0; i< size; i++){
  17.         printf("I = %d\n", i);
  18.         char buf[MAX];
  19.         int lim = 0;
  20.         printf("Введите строку: ");
  21.         fgets(buf, MAX, stdin);
  22.  
  23.         printf("string is: %s\n", buf);
  24.         for (int i = 0; i < MAX; i++)
  25.         {
  26.             if (buf[i] == '\n'){
  27.                 lim = i;
  28.                 break;
  29.             }
  30.         }
  31.         printf("%d\n",lim);
  32.         char line[lim];
  33.         for (int i = 0; i < lim; i++){
  34.             line[i] = buf[i];
  35.            
  36.         }
  37.         printf("line = %s\n", line);
  38.         p[i] = *(char *) malloc(lim + 1 * sizeof(char));
  39.         strcpy(&p[i], line);
  40.         printf("%s\n", &p[i]);
  41.     }
  42.  
  43.     for (int i = 0; i < size; i++){
  44.         printf("%s\n", p[i]);
  45.     }
  46.  
  47. }
  48.  
  49. int main(){
  50. arrayOfChars();
  51.  
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment