82 lines
1.9 KiB
C
82 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_NAME_LENGTH 100
|
|
|
|
int compare(const void *a, const void *b) {
|
|
return strcmp(*(const char **)a, *(const char **)b);
|
|
}
|
|
|
|
void free_memory(char **array, int length) {
|
|
for (int i = 0; i < length; i++) {
|
|
free(array[i]);
|
|
}
|
|
free(array);
|
|
}
|
|
|
|
int main() {
|
|
int max_students;
|
|
char name[MAX_NAME_LENGTH];
|
|
char **students = NULL;
|
|
int num_students = 0;
|
|
|
|
|
|
printf("Enter the maximum number of students that can be accepted: ");
|
|
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
|
|
puts("Invalid input for the number of students.");
|
|
return 1;
|
|
}
|
|
|
|
|
|
students = (char **)malloc(max_students * sizeof(char *));
|
|
if (students == NULL) {
|
|
puts("Memory allocation failed.");
|
|
return 1;
|
|
}
|
|
|
|
printf("Enter student names (press Enter twice to stop):\n");
|
|
while (fgets(name, MAX_NAME_LENGTH, stdin) && num_students < max_students) {
|
|
name[strcspn(name, "\n")] = '\0';
|
|
if (strlen(name) == 0) {
|
|
break;
|
|
}
|
|
|
|
|
|
int duplicate = 0;
|
|
for (int i = 0; i < num_students; i++) {
|
|
if (strcmp(students[i], name) == 0) {
|
|
duplicate = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!duplicate) {
|
|
students[num_students] = strdup(name);
|
|
if (students[num_students] == NULL) {
|
|
puts("Memory allocation failed.");
|
|
free_memory(students, num_students);
|
|
return 1;
|
|
}
|
|
num_students++;
|
|
}
|
|
}
|
|
|
|
if (num_students == 0) {
|
|
puts("No applications were received.");
|
|
free(students);
|
|
return 1;
|
|
}
|
|
|
|
qsort(students, num_students, sizeof(char *), compare);
|
|
|
|
puts("Accepted students:");
|
|
for (int i = 0; i < num_students; i++) {
|
|
puts(students[i]);
|
|
}
|
|
|
|
free_memory(students, num_students);
|
|
|
|
return 0;
|
|
}
|