Originally Posted by
Ash Mo Naiming
i want to a C program to delete an element from an array which can use both index method & character method
For example
input : "1 222 333 4444"
output:"1 22 333 4444"
if either index = "4" or character ="2" is entered
This is what "memmove" and strchr, and strrchr are for. strchr and strrchr return a pointer to where a certain character is. So define a delete_elem function to take a pointer argument like this
Code:
void delete_elem(char *arr, size_t n, char *s) {
memmove(s, s+1, n-(s-arr));
}
The s points to where you want to delete.
Code:
char telephone[] = "1 234 456 6789";
// delete character at index 2
delete_elem(telephone, sizeof(telephone), &telephone[2]);
// delete first occurence of '4'
delete_elem(telephone, sizeof(telephone), strchr(telephone, '4'));
// delete last occurence of '6'
delete_elem(telephone, sizeof(telephone), strrchr(telephone, '6'));