In C, you can iterate through the array and move elements to fill the gaps created by removing elements based on a condition.
In this example,
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int length = sizeof(numbers) / sizeof(numbers[0]);
int i, count = 0;
for (i = 0; i < length; i++) {
if (numbers[i] % 2 != 0) {
numbers[count++] = numbers[i];
}
}
printf("Array after filtering:\n");
for (i = 0; i < count; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Array after filtering: 1 3 5 7 9
In this example,
#include <stdio.h>
#include <string.h>
int main() {
char words[][10] = {"apple", "banana", "grape", "orange", "melon"};
int length = sizeof(words) / sizeof(words[0]);
int i, count = 0;
for (i = 0; i < length; i++) {
if (!strchr(words[i], 'a')) {
strcpy(words[count++], words[i]);
}
}
printf("Array after filtering:\n");
for (i = 0; i < count; i++) {
printf("%s ", words[i]);
}
return 0;
}
Array after filtering: melon
In this tutorial, we learned How to Remove Element from Array based on a Condition in C language with well detailed examples.