pvjc24/cv10/program.c

66 lines
1.5 KiB
C
Raw Normal View History

2024-04-22 12:01:17 +00:00
#include <stdio.h>
2024-04-25 16:32:35 +00:00
#include <stdlib.h>
2024-04-25 15:29:10 +00:00
#include <string.h>
2024-04-22 12:01:17 +00:00
2024-04-25 17:04:21 +00:00
int compare_names(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
2024-04-22 12:01:17 +00:00
int main() {
2024-04-25 16:42:45 +00:00
int count;
char buffer[100];
char **applications;
2024-04-25 18:03:57 +00:00
int i;
2024-04-22 12:06:40 +00:00
2024-04-25 16:42:45 +00:00
if (scanf("%d", &count) != 1 || count <= 0) {
2024-04-25 15:32:41 +00:00
printf("Nespravny vstup\n");
2024-04-22 12:06:40 +00:00
return 1;
}
2024-04-22 12:01:17 +00:00
2024-04-25 16:42:45 +00:00
applications = (char **)malloc(count * sizeof(char *));
if (!applications) {
printf("Chyba pri alokacii pamate\n");
return 1;
2024-04-25 16:32:35 +00:00
}
2024-04-25 15:41:03 +00:00
2024-04-25 16:42:45 +00:00
i = 0;
2024-04-25 17:44:24 +00:00
while (i < count && fgets(buffer, sizeof(buffer), stdin)) {
2024-04-25 16:49:15 +00:00
int len = strlen(buffer);
2024-04-25 16:56:41 +00:00
if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present
2024-04-25 16:49:15 +00:00
buffer[len - 1] = '\0';
}
2024-04-25 16:42:45 +00:00
int found = 0;
2024-04-25 18:03:57 +00:00
for (int j = 0; j < i; j++) {
2024-04-25 16:42:45 +00:00
if (strcmp(applications[j], buffer) == 0) {
found = 1;
break;
}
}
if (!found) {
applications[i] = strdup(buffer);
i++;
}
}
if (i == 0) {
2024-04-25 16:32:35 +00:00
printf("Ziadne prihlasky\n");
return 1;
}
2024-04-25 15:56:47 +00:00
2024-04-25 16:42:45 +00:00
// Sort the applications alphabetically
2024-04-25 17:04:21 +00:00
qsort(applications, i, sizeof(char *), compare_names);
2024-04-22 12:01:17 +00:00
2024-04-25 18:02:38 +00:00
printf("Prijati studenti:\n");
2024-04-25 18:03:57 +00:00
for (int j = 0; j < i; j++) {
2024-04-25 16:42:45 +00:00
printf("%s\n", applications[j]);
2024-04-22 12:01:17 +00:00
}
2024-04-25 16:42:45 +00:00
// Free allocated memory
2024-04-25 18:03:57 +00:00
for (int j = 0; j < i; j++) {
2024-04-25 16:42:45 +00:00
free(applications[j]);
}
free(applications);
2024-04-22 12:01:17 +00:00
return 0;
2024-04-25 18:02:38 +00:00
}