diff --git a/01_modultechnik/main.c b/01_modultechnik/main.c index d172398..4d5b2dc 100644 --- a/01_modultechnik/main.c +++ b/01_modultechnik/main.c @@ -45,7 +45,7 @@ int main() printf("\nNach sortierter Eingabe:\n"); printNames(); - printf("\n"); + printf("\n");*/ while((inputName("Namen entfernen: ", buffer, MAX_BUFFER_LEN) != NULL) && (strlen(buffer) > 0)) { @@ -56,7 +56,7 @@ int main() } printf("\nNach Entfernen:\n"); - printNames();*/ + printNames(); return EXIT_SUCCESS; } diff --git a/01_modultechnik/namesarchive.c b/01_modultechnik/namesarchive.c index a0a071d..061fd60 100644 --- a/01_modultechnik/namesarchive.c +++ b/01_modultechnik/namesarchive.c @@ -3,6 +3,8 @@ #include #include +#define INDEX_NOT_FOUND -1 + static char names[MAX_NAMES][MAX_NAME_LEN+1]; static unsigned int numberOfEntries = 0; @@ -39,4 +41,39 @@ void printNames() { printf("%s\n", names[i]); } +} + +static int getNameIdx(const char *name) +{ + for(int i = 0; i < numberOfEntries; i++) + { + if(strcmp(name, names[i]) == 0) + return i; + } + return INDEX_NOT_FOUND; +} + +static void removeAt(int idx) +{ + for(int i = idx; i < numberOfEntries-1; i++) + { + strncpy(names[i], names[i+1], MAX_NAME_LEN+1); + names[i][MAX_NAME_LEN] = '\0'; + } + numberOfEntries--; +} + +int removeName(const char *name) +{ + int idx = getNameIdx(name); + + if(idx == INDEX_NOT_FOUND) + { + return 0; + } + else + { + removeAt(idx); + return 1; + } } \ No newline at end of file