Remove names.
This commit is contained in:
parent
c7631b1a14
commit
63ca042679
@ -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;
|
||||
}
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define INDEX_NOT_FOUND -1
|
||||
|
||||
static char names[MAX_NAMES][MAX_NAME_LEN+1];
|
||||
static unsigned int numberOfEntries = 0;
|
||||
|
||||
@ -40,3 +42,38 @@ 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user