// Arup Guha
// 1/19/2022
// Code for COP 3502 Quiz #1 Version A

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct artifact {
    int x;
    int y;
} artifact;

int* getStringLengths(char** words, int n);
int* makePalArray(int array[], int n);

int main(void) {

    // Question #1 - hard to test this by itself.
    int n;
    scanf("%d", &n);
    artifact** arr = calloc(n, sizeof(artifact*));
    for (int i=0; i<n; i++)
        arr[i] = NULL;

    // Not part of answer...
    free(arr);

    // Question #2 test case
    int test[10] = {2,7,6,1,9,10,3,5,8,4};
    int* rest = makePalArray(test, 10);
    for (int i=0; i<20; i++) printf("%d ", rest[i]);
    printf("\n");

    // Not part of answer.
    free(rest);

    // Question #3 test case.
    char** words = malloc(sizeof(char*)*6);
    for (int i=0; i<6; i++) words[i] = malloc(20);
    strcpy(words[0], "hello");
    strcpy(words[1], "jasmine");
    strcpy(words[2], "how");
    strcpy(words[3], "are");
    strcpy(words[4], "you");
    strcpy(words[5], "doing?");
    int* strlens = getStringLengths(words, 6);
    for (int i=0; i<6; i++) printf("%d ", strlens[i]);
    printf("\n");

    // Not part of answer.
    for (int i=0; i<6; i++) free(words[i]);
    free(words);
    free(strlens);

    // Question #4
    int len1, len2;
    scanf("%d%d", &len1, &len2);
    char* str1 = malloc(len1*sizeof(char));
    char* str2 = malloc(len2*sizeof(char));
    scanf("%s%s", str1, str2);

    // 4 pts for this calculation.
    int need = strlen(str1) + strlen(str2) + 1;

    // 3 pts for this realloc. LHS and assignment unnecessary.
    str1 = realloc(str1, need);

    strcat(str1, str2);
    printf("%s %s\n", str1, str2);
    free(str1);
    free(str2);

    return 0;
}

// Question #3
int* getStringLengths(char** words, int n) {

    // 4 pts, can use calloc or malloc.
    int* res = malloc(n*sizeof(int));

    // 2 pts for loop, 2 pts for assignment
    for (int i=0; i<n; i++)
        res[i] = strlen(words[i]);

    // 2 pts for returning correct pointer.
    return res;
}

// Question #2
int* makePalArray(int array[], int n) {

    // 3 pts, can use calloc or malloc.
    int* res = calloc(2*n, sizeof(int));

    // 2 pts
    for (int i=0; i<n; i++)
        res[i] = array[i];

    // 2 pts for loop, 2 pts for assignment
    for (int i=n; i<2*n; i++)
        res[i] = array[2*n-1-i];

    // 1 pt for returning correct pointer.
    return res;
}

