---
# System prepended metadata

title: Solving problem

---

# Solving problem

## Problem Description
Solve this programming problem in C language:
Let us have

typedef struct date { int day; char month[4]; int year; }DATE; typedef struct student { char* pName, * pCode; DATE Date; int Mark; }STUDENT;

Write a function with prototype: STUDENT** Exam(char* pNames, char* pResults, int* pnStudents);

where pnStudent points to the number of students (output value of our function, see slide Pointers(21) and pNameCodes and pResults poiunt to strings like: char NamesCodes[]= "John Smith 25156IVSB; Mary Weaver 253742ABB; James Carpenter 252422TAF; Elizabeth Clerk 251443MV"; char Results[]= "Elizabeth Clerk 4; Mary Weaver 5; John Smith 1; James Carpenter 3";

The components of strings are separated byt semicolon followed by one space. The names are everywhere the same but their orders may be different. Turn attention that there is no semicolon after the last component.

The output vlaue is an array of pointers to structs (see slide Structs(19). The order of students in results may be any.

The value attributes in DATE must be read from the system clock.

The input values may not be zero or point to an empty array string. If those conditions are not satisfied, the function must return zero.

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

// Define the date struct
typedef struct date {
    int day;
    char month[4];
    int year;
} DATE;

// Define the student struct
typedef struct student {
    char* pName, * pCode;
    DATE Date;
    int Mark;
} STUDENT;

// Declare the Exam function
STUDENT** Exam(char* pNames, char* pResults, int* pnStudents);

// Implement the Exam function
STUDENT** Exam(char* pNames, char* pResults, int* pnStudents) {
    // Check if the input values are valid
    if (pNames == 0 || pResults == 0 || pnStudents == 0) {
        return 0; // Return 0 if the input values are invalid
    }

    // Get the current date from the system clock
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    // Split the input strings by '; ' to get an array of names and codes
    char* namesCodes = strtok(pNames, "; ");
    char** pNamesCodes = malloc(sizeof(char*));
    int n = 0; // Keep track of the number of names and codes
    while (namesCodes != NULL) {
        pNamesCodes[n] = namesCodes;
        n++;
        pNamesCodes = realloc(pNamesCodes, (n + 1) * sizeof(char*));
        namesCodes = strtok(NULL, "; ");
    }

    // Split the input string by '; ' to get an array of results
    char* result = strtok(pResults, "; ");
    char** pResults = malloc(sizeof(char*));
    n = 0; // Keep track of the number of results
    while (result != NULL) {
        pResults[n] = result;
        n++;
        pResults = realloc(pResults, (n + 1) * sizeof(char*));
        result = strtok(NULL, "; ");
    }

    // Create an array of pointers to student structs
    STUDENT** pStudents = malloc(n * sizeof(STUDENT*));

    // Loop through the results and create a student struct for each result
    for (int i = 0; i < n; i++) {
        // Create a student struct
        pStudents[i] = malloc(sizeof(STUDENT));

        // Split the result string by ' ' to get the name and mark
        char* name = strtok(pResults[i], " ");
        char* markStr = strtok(NULL, " ");
        pStudents[i]->Mark = atoi(markStr); // Convert the mark from string to int

        // Loop through the names and codes and find the matching name and code
        for (int j = 0; j < n; j++) {
            // Split the name and code string by ' ' to get the name and code
            char* name2 = str

```