pvjc24/cv10/program.c
2024-04-25 21:15:42 +02:00

74 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare_names(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
int count;
char buffer[100];
char **applications;
int i, j, accepted_count = 0;
if (scanf("%d", &count) != 1 || count <= 0) {
printf("Nespravny vstup\n");
return 1;
}
applications = (char **)malloc(count * sizeof(char *));
if (!applications) {
printf("Chyba pri alokacii pamate\n");
return 1;
}
i = 0;
while (i < count && fgets(buffer, sizeof(buffer), stdin)) {
int len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present
buffer[len - 1] = '\0';
}
int found = 0;
for (j = 0; j < i; j++) {
if (strcmp(applications[j], buffer) == 0) {
found = 1;
break;
}
}
if (!found) {
applications[i] = strdup(buffer);
i++;
accepted_count++;
}
}
if (accepted_count == 0) {
printf("Ziadne prihlasky\n");
return 1;
}
// Sort the applications alphabetically
qsort(applications, accepted_count, sizeof(char *), compare_names);
printf("Prijati studenti:");
for (j = 0; j < count && j < accepted_count; j++) {
printf("%s\n", applications[j]);
}
if (count < accepted_count) {
printf("Neprijati studenti:");
for (; j < i; j++) {
printf("%s\n", applications[j]);
}
}
// Free allocated memory
for (j = 0; j < i; j++) {
free(applications[j]);
}
free(applications);
return 0;
}