This commit is contained in:
Bora Zuenbuelkoek 2025-05-02 08:16:35 +02:00
parent 50d0fcdb6d
commit 48ba00f7bb
5 changed files with 55 additions and 2 deletions

2
.idea/Info2P5.iml generated
View File

@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CIDR" type="CPP_MODULE" version="4" />

BIN
a.exe

Binary file not shown.

55
strilist.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_DIGITS 10
int main(void) {
char *strichlisten[NUM_DIGITS] = {NULL};
int counts[NUM_DIGITS] = {0};
int c;
// Initialisiere alle Strichlisten als leere Strings
for (int i = 0; i < NUM_DIGITS; ++i) {
strichlisten[i] = malloc(1);
if (!strichlisten[i]) {
fprintf(stderr, "Speicherfehler!\n");
// Speicher aufräumen
for (int j = 0; j < i; ++j) free(strichlisten[j]);
return 1;
}
strichlisten[i][0] = '\0';
}
// Einlesen der Ziffern
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9') {
int idx = c - '0';
counts[idx]++;
// Länge des alten Strings
size_t oldlen = strlen(strichlisten[idx]);
// Speicher für neuen String (+1 für neuen Strich, +1 für '\0')
char *neu = malloc(oldlen + 2);
if (!neu) {
fprintf(stderr, "Speicherfehler!\n");
// Speicher aufräumen
for (int j = 0; j < NUM_DIGITS; ++j) free(strichlisten[j]);
return 1;
}
strcpy(neu, strichlisten[idx]);
neu[oldlen] = '|';
neu[oldlen+1] = '\0';
free(strichlisten[idx]);
strichlisten[idx] = neu;
}
}
// Ausgabe
for (int i = 0; i < NUM_DIGITS; ++i) {
printf("%d: %d %s\n", i, counts[i], strichlisten[i]);
free(strichlisten[i]);
}
return 0;
}

Binary file not shown.

Binary file not shown.