---
# System prepended metadata

title: 13804 - Make more sentences
tags: [dynamic memory]

---

# 13804 - Make more sentences

>author: Utin
###### tags: `dynamic memory`

---

## Brief
See the code below

## Solution 0

```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N, M, length[101];
char* student[101];

int main() {
    scanf("%d %d", &N, &M);

    // student sentence
    for (int n = 0; n < N; n++) {
        char temp[10001];
        scanf("%s", temp);
        length[n] = strlen(temp);
        student[n] = (char*) malloc(length[n] + 1);
        memset(student[n], 0, length[n] + 1);
        strcpy(student[n], temp);
    }

    // operation
    while (M--) {
        char op[15];
        scanf("%s", op);

        if (op[0] == 'r') {
            int x;
            scanf("%d", &x);
            x--;

            // make the new string
            char* temp = (char*) malloc(length[x] + 1);
            memset(temp, 0, length[x] + 1);
            for (int i = 0; i < length[x]; i++) {
                temp[i] = student[x][length[x] - i - 1];
            }

            // free the old string and point to the new one
            free(student[x]);
            student[x] = temp;
        }

        else if (op[0] == 'p') {
            int x;
            scanf("%d", &x);
            x--;
            printf("%s\n", student[x]);
        }

        else if (op[0] == 'a') {
            int x, y;
            scanf("%d %d", &x, &y);
            x--;
            y--;

            // make the new string
            char* temp = (char*) malloc(length[x] + length[y] + 1);
            memset(temp, 0, length[x] + length[y] + 1);
            if (length[y] < length[x]) {
                strcpy(temp, student[y]);
                strcat(temp, student[x]);

                // free the old string and point to the new one
                free(student[y]);
                student[y] = temp;

                // modify the length
                length[y] += length[x];
            }
            else {
                strcpy(temp, student[x]);
                strcat(temp, student[y]);

                // free the old string and point to the new one
                free(student[x]);
                student[x] = temp;

                // modify the length
                length[x] += length[y];
            }
        }

        else if (op[0] == 'd') {
            char s[10001];
            int x, s_len;
            scanf("%s %d", s, &x);
            x--;
            s_len = strlen(s);

            char* flag = strstr(student[x], s);
            if (flag != NULL) {

                // make the new string
                char* temp = malloc(length[x] - s_len + 1);
                memset(temp, 0, length[x] - s_len + 1);
                strncpy(temp, student[x], flag - student[x]);
                strcat(temp, flag + s_len);

                // free the old string and point to the new one
                free(student[x]);
                student[x] = temp;

                // modify the length
                length[x] -= s_len;
            }
        }
    }
}
// By Utin
```

## Reference